题目描述
1、建立如下的类继承结构:
1)一个车类CVehicle作为基类,具有max_speed、speed、weight等数据成员,display()等成员函数
2)从CVehicle类派生出自行车类CBicycle,添加属性:高度height
3)从CVehicle类派生出汽车类CMotocar,添加属性:座位数seat_num
4)从CBicycle和CMotocar派生出摩托车类CMotocycle
2、分别定义以上类的构造函数、输出函数display及其他函数(如需要)。
3、在主函数中定义各种类的对象,并测试之,通过对象调用display函数产生输出。
输入
第一行:最高速度 速度 重量 第二行:高度 第三行:座位数
输出
第一行:Vehicle: 第二行及以后各行:格式见Sample
100 60 20
28
2
Vehicle:
max_speed:100
speed:60
weight:20
Bicycle:
max_speed:100
speed:60
weight:20
height:28
Motocar:
max_speed:100
speed:60
weight:20
seat_num:2
Motocycle:
max_speed:100
speed:60
weight:20
height:28
seat_num:2
#include
using namespace std;
class CVehicle
{
protected:
int max_speed;
int speed;
int weight;
public:
CVehicle(){
}
CVehicle(int ms, int s, int w):max_speed(ms),speed(s),weight(w){
}
void display()
{
cout<<"Vehicle:"<<endl;
cout<<"max_speed:"<<max_speed<<endl;
cout<<"speed:"<<speed<<endl;
cout<<"weight:"<<weight<<endl;
cout<<endl;
}
};
class CBicycle:public CVehicle
{
protected:
int height;
public:
CBicycle(int ms, int s, int w, int h):CVehicle(ms,s,w),height(h){
}
void display()
{
cout<<"Bicycle:"<<endl;
cout<<"max_speed:"<<max_speed<<endl;
cout<<"speed:"<<speed<<endl;
cout<<"weight:"<<weight<<endl;
cout<<"height:"<<height<<endl;
cout<<endl;
}
};
class CMotocar:public CVehicle
{
protected:
int seat_num;
public:
CMotocar(int ms, int s, int w, int seat):CVehicle(ms,s,w){
seat_num = seat; }
void display(){
cout<<"Motocar:"<<endl;
cout<<"max_speed:"<<max_speed<<endl;
cout<<"speed:"<<speed<<endl;
cout<<"weight:"<<weight<<endl;
cout<<"seat_num:"<<seat_num<<endl;
cout<<endl;
}
};
class CMotocycle:public CBicycle, public CMotocar
{
public:
CMotocycle(int ms, int s, int w, int h, int seat):CBicycle(ms,s,w,h),CMotocar(ms,s,w,seat){
}
void display(){
cout<<"Motocycle:"<<endl;
cout<<"max_speed:"<<CBicycle::max_speed<<endl;
cout<<"speed:"<<CBicycle::speed<<endl;
cout<<"weight:"<<CBicycle::weight<<endl;
cout<<"height:"<<height<<endl;
cout<<"seat_num:"<<seat_num<<endl;
cout<<endl;
}
};
int main()
{
int max_speed,speed,weight,height,seat_num;
cin>>max_speed>>speed>>weight;
cin>>height;
cin>>seat_num;
CVehicle vehicle(max_speed,speed,weight);
CBicycle bicycle(max_speed,speed,weight,height);
CMotocar motocar(max_speed,speed,weight,seat_num);
CMotocycle motocycle(max_speed,speed,weight,height,seat_num);
vehicle.CVehicle::display();
bicycle.CBicycle::display();
motocar.CMotocar::display();
motocycle.CMotocycle::display();
}
#include
#include
using namespace std;
class CVehicle
{
protected:
int max_speed, speed, weight;
public:
CVehicle(int ms, int s, int w) :max_speed(ms), speed(s), weight(w)
{
}
~CVehicle() {
}
void display()
{
cout << "Vehicle:" << endl;
cout << "max_speed:" << max_speed << endl;
cout << "speed:" << speed << endl;
cout << "weight:" << weight << endl;
cout << endl;
}
};
class CBicycle :virtual public CVehicle
{
protected:
int height;
public:
CBicycle(int h, int ms, int s, int w) :height(h), CVehicle(ms, s, w)
{
}
~CBicycle() {
}
void display()
{
cout << "Bicycle:" << endl;
cout << "max_speed:" << max_speed << endl;
cout << "speed:" << speed << endl;
cout << "weight:" << weight << endl;
cout << "height:" << height << endl;
cout << endl;
}
};
class CMotocar :virtual public CVehicle
{
protected:
int seat_num;
public:
CMotocar(int sn, int ms, int s, int w) :seat_num(sn), CVehicle(ms, s, w)
{
}
~CMotocar() {
}
void display()
{
cout << "Motocar:" << endl;
cout << "max_speed:" << max_speed << endl;
cout << "speed:" << speed << endl;
cout << "weight:" << weight << endl;
cout << "seat_num:" << seat_num << endl;
cout << endl;
}
};
class CMotocycle :public CBicycle, public CMotocar
{
protected:
public:
CMotocycle(int h, int sn, int ms, int s, int w) :CVehicle(ms, s, w), CBicycle(h, ms, s, w), CMotocar(sn, ms, s, w)
{
}
~CMotocycle() {
}
void display()
{
cout << "Motocycle:" << endl;
cout << "max_speed:" << max_speed << endl;
cout << "speed:" << speed << endl;
cout << "weight:" << weight << endl;
cout << "height:" << height << endl;
cout << "seat_num:" << seat_num << endl;
}
};
int main()
{
int max_speed, speed, weight, height, seat_num;
cin >> max_speed >> speed >> weight >> height >> seat_num;
CMotocycle sb(max_speed, speed , weight,height,seat_num);
sb.CVehicle::display();
sb.CBicycle::display();
sb.CMotocar::display();
sb.CMotocycle::display();
return 0;
}