Data Structures and Other Objects Using C++ (Chapter 2) 学习笔记四

来源:互联网 发布:开淘宝网店需要的软件 编辑:程序博客网 时间:2024/06/05 20:49

Chapter 2   Abstract Data Types and C++ Classes

2.1 CLASSES AND MEMBERS

     A class is a new kind of data type. Each class that you define is a collection of data, and has the ability to include member functions. Member functions are incorporated into the class's definition and are designed specifically to manipulate the class.
PROGRAMMING EXAMPLE: The Throttle Class
     
class throttle{  public:           // MODIFICATION MEMBER FUNCTIONS    void shut_off ();    void shift (int amount);           // CONSTANT MEMBER FUNCTIONS     double flow() const;     bool is_on() const;  private:     int position;};
A class has some sections as follows :
    The class head. The head of the definition consists of the C++ keyword class, followed by the name of the new class.
    The member list. 
    The public section.
          modification functions.
          constant member functions:may examine the status of its object, but it is forbidden from changing the object.
     The private section.

Using a Class

    declare objects of a class:
         throttle my_throttle;
         throttle control;
 Every throttle object contains the private member variable position, but there is no way for a program to access this component (组件)directly, because it is a private member. The only way that a program can use its throttle objects is by using the public member functions. such as: control.shut_off(); control.shift(3);
    calling a member function always involves the following four steps: 
  • Start with the name of the object that you are manipulating.
  • After the object name, place a single period.
  • Next, write the name of the member function. 
  • Finally, list the arguments for the function call.

A Small Demonstration Program for the Throttle Class

// FILE: demol.cxx// This small demonstration shows how the throttle class is used.#include <iostream>#include <cstdlib>using namespace std;class throttle{     public:         // MODIFICATION MEMBER FUNCTIONS         void shut_off ();         void shift (int amount);         // CONSTANT MEMBER FUNCTIONS         double flow() const;         bool is_on () const;     private:   int position;};int main(){     throttle sample;     int user_input;     // Set the sample throttle to a position indicated by the user.     cout << "I have a throttle with 6 positions." << endl;     cout << "Where would you like to set the throttle? " << endl;     cout << "Please type a number from 0 to 6: ";     cin >> user_input;     sample.shut_off();     sample.shift (user_input);     // shift the throttle down to zero, printing the flow along the way.     while (sample.is_on())     {          cout << "The flow is now " << sample.flow()  << endl;          sample.shift(-1);      }     cout << "The folw is now off" << endl;     return EXIT_SUCCESS;}

Implementing Member Functions
void throttle::shut_off()// precondition: None// Postcondition: The throttle has been turned off.{     position = 0;}

void throttle::shift (int amount)// Precondition: shut_off has been called at least once to initialize the throttle.// Postcondition: The throttle's position has been moved by amount{      position += amount;      if (position < 0)          position = 0;      else if (position > 6)          position = 6;}

double throttle::flow() const// Precondition: shut_off has been called at least once to initialize the throttle.// Postcondition: The value returned is the current flow as a proportion of the max flow.{       return position / 6.0;}

bool throttle::is_on() const // Precondition: shut_off has been called at least once to initialize the throttle.// Postcondition: If the throttle's flow is above 0, // then the function returns true; otherwise, it returns false.{       return (flow() > 0);}



  
原创粉丝点击