#include <iostream>
#include<string.h>
#include<math.h>
using namespace::std;
class CVehicle
{
public:
CVehicle(int max_speed1, int speed1, int weight1) { max_speed = max_speed1; speed = speed1; weight = weight1; }
int max_speed;
int speed;
int weight;
CVehicle();
~CVehicle();
void CVehicledisplay();
void display1() {
cout << "Vehicle" << ":" << endl;
cout << "max_speed" << ":" << max_speed << endl;
cout << "speed" << ":" << speed << endl;
cout << "weight" << ":" << weight << endl;
cout << endl;
}
};
CVehicle::CVehicle(){}
CVehicle::~CVehicle(){}
class CBicycle :virtual public CVehicle
{
public:
CBicycle();
CBicycle(int max_speed1, int speed1, int weight1, int height1) :CVehicle(max_speed1, speed1, weight1) { height = height1; }
int height;
void display2() {
cout << "Bicycle" << ":" << endl;
cout << "max_speed" << ":" << max_speed << endl;
cout << "speed" << ":" << speed << endl;
cout << "weight" << ":" << weight << endl;
cout << "height" << ":" << height << endl;
cout << endl;
}
};
CBicycle::CBicycle(){}
class CMotocar :virtual public CVehicle
{
public:
CMotocar();
CMotocar(int max_speed1, int speed1, int weight1, int seat_num1) :CVehicle(max_speed1, speed1, weight1) { seat_num = seat_num1; }
int seat_num;
void display3() {
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;
}
};
CMotocar::CMotocar(){}
class CMotocycle : public CBicycle, public CMotocar
{
public:
CMotocycle();
CMotocycle(int max_speed1, int speed1, int weight1, int height1, int seat_num1) :CVehicle(max_speed1, speed1, weight1) ,CBicycle(max_speed1, speed1, weight1,height1) ,CMotocar(max_speed1, speed1, weight1,seat_num1){ seat_num = seat_num1; } //注意此处的CBicycle和CMotocar的调用数量,应该与先前一致。
void CMotocycledisplay();
void display4() {
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;
cout << endl;
}
};
CMotocycle::CMotocycle() {}
int main()
{
int max_speed1;
int speed1;
int weight1;
int height1;
int seat_num1;
cin >> max_speed1 >> speed1 >> weight1;
cin >> height1;
cin >> seat_num1;
CMotocycle c(max_speed1,speed1, weight1, height1, seat_num1);
c.display1();
c.display2();
c.display3();
c.display4();
}
一开始写了一堆set函数,发现其实用构造函数就能解决全部问题,跟OJ上提交的不同,在此处优化了一下,更加简洁,问题就在于我仍然全都是public成员,习惯挺差的说实话。