Abstract data types and C ++

Class is a new data type, similar to the data structure, but it has some of the data structures that are not - "member functions", precisely because it has a member function of this feature, so that it can hide "data structure "data (class), it is not known to the user. By class member function, so as to achieve the connection classes are carried out by a member function implemented. Next we discussed this concept with an example:

Example: class throttle valve: Throttle

  The throttle valve, surely we are more familiar with it by moving the joystick to control the inflow and outflow of liquid, that is, to control the flow, the position where the lever and then there is a correlation between the flow rate through the throttle valve. By controlling the position of the control lever so that it can control the flow through the throttle valve, the throttle valve 10 assume positions, when the position of the maximum flow rate is 10, the flow rate is zero is zero. Therefore, according to the characteristics of the throttle valve, so that we can list some of the features:

1, current location: position

2, the current flow rate: flow (correlation, using the maximum flow rate ratio of the current flow is represented as the ratio of maximum flow position of the current position in accordance)

3, the throttle valve is currently closed: is_no

4, the control lever moved to what position: shift

5, the throttle valve is closed shut_off

 

So we can build up a throttle category:

Throttle {class
    public:
      void shut_off (); // initialize the current position, the closing operation of the throttle valve
      void shift (int amount); // movement of the input throttle position
      double flow () const; // outputs the maximum position the ratio of the current position

      bool is_no () const; // determines whether an ON state
    Private:
      int position;
};

 

According to a member function thus provided, we made in the specific member function were written:

void throttle::shut_off(){
  position=0;
}

void throttle::shift(int amount){
  position+=amount;
  if(position<0){
    position=0;
  }else if(position>10){
    position=10;
  }
}

double throttle::flow()const {
  return position/10.0;
}

bool throttle::is_no()const{
  return (flow()>0);
}

This is what we will be able to carry out specific conditions of use of this class; if there is a swimming pool, now need to turn on the water by the throttle position changes every time one hour of a throttle during turn on the water in, then amount required to obtain a given position, the output of its hourly traffic volume?

int main()
{
  throttle sample;
  int user_input;
  cout<<"I have a throttle with 10 position."<<endl;
  cout<<"Where would you like to set the throttle?"<<endl;
  cout<<"Please type a number 0 to 10.";
  cin>>user_input;
  sample.shut_off();
  sample.shift(user_input);

  while(sample.is_no()){
    cout<<"The flow is now "<<sample.flow()<<endl;
    sample.shift(-1);
  }
  cout<<"The flow is now off"<<endl;
  return EXIT_SUCCESS;

}

The results are as follows:

 

Guess you like

Origin www.cnblogs.com/Justina/p/11230987.html