#include <iostream>
using namespace std;
/* run this program using the console pauser or add your own getch, system("pause") or input loop */
//定义基类
class Vehicle
{
public:
//定义构造函数
Vehicle( double maxSpeed, double weight ){
this->maxSpeed = maxSpeed;
this->weight = weight;
}
//定义成员函数
double GetMaxSpeed(){ return maxSpeed; }
double GetWeight(){ return weight; }
protected:
//定义数据成员
double maxSpeed;
double weight;
};
//定义派生类, 这里采用公有继承
class Bicycle : public Vehicle
{
public:
//定义派生类,注意基类初始化
Bicycle( double maxSpeed, double weight, double height) : Vehicle( maxSpeed, weight )
{
this->height = height;
}
//定义派生类成员函数
void Run(){
cout<<"The Bicycle is running!"<<endl;
}
void Stop(){
cout<<"The Bicycle is stop!"<<endl;
}
protected:
double height;
};
class Motorcar : public Vehicle
{
public:
Motorcar( double maxSpeed, double weight, int seatNum) : Vehicle( maxSpeed, weight )
{
this->seatNum = seatNum;
}
void Run(){
cout<<"The Motorcar is running!"<<endl;
}
void Stop(){
cout<<"The Motorcar is stop!"<<endl;
}
protected:
int seatNum;
};
int main(int argc, char** argv) {
//定义类对象
Bicycle b(1,2,3);
Motorcar m(4,5,6);
//调用基类函数成员
cout<<"Bicycle :maxSpeed="<<b.GetMaxSpeed()<<" weight="<<b.GetWeight()<<endl;
cout<<"Motorcar :maxSpeed="<<m.GetMaxSpeed()<<" weight="<<m.GetWeight()<<endl;
//调用派生类函数成员
b.Run();
b.Stop();
m.Run();
m.Stop();
return 0;
}
C++ work3
最新推荐文章于 2024-05-20 15:29:54 发布