#include
using namespace std;
//造一个简单的类
class motor
{
public:
motor(int speedt,int statust);
void motor_start(void);
void motor_speed_up(void);
void motor_status(void);
private:
int speed,status;
};
//定义构造函数
motor::motor(int speedt = 0,int statust = 0)
{
speed = speedt;
status = statust;
}
//定义成员函数
void motor::motor_speed_up()
{
if(speed <= 2000)
speed += 2;
}
void motor::motor_start()
{
status = 1;
}
void motor::motor_status()
{
cout<<"speed:"<<speed<<endl;
cout<<"status:"<<status<<endl;
}
motor m0,m1(100,1);//声明两个对象。分配内存空间(两个对象的成员数据分开,成员函数共享一套代码)。
int main(void)
{
int temp;
cout<<"start the motor m0 and m1"<<endl;
m0.motor_start();
m1.motor_start();
cout<<" motor m0 speed up"<<endl;
m0.motor_speed_up();
cout<<"the speed of m0 is:"<<endl;
m0.motor_status();
cout<<"the speed of m1 is:"<<endl;
m1.motor_status();
cin>>temp;
}