- 设计一个汽车类vehicle,数据成员包括:车轮个数、车重。小车类car是它的公有派生类,新增数据成员:载人数passenger;卡车truck是汽车类的公有派生类,新增数据成员:载人数passenger和载重量:payload,另外每个类都有相关数据输出方法,其他数据和方法可以自行添加,请设计实现这三个类,并编写测试代码进行测试。
#include <iostream>
#include <stdlib.h>
#include <string>
using namespace std;
class vehicle
{
protected:
int wheels;
double weight;
public:
vehicle(int a,double b);
int GetWheels() { return wheels; }
double GetWeight() { return weight; }
void show();
};
vehicle::vehicle(int a, double b)
{
wheels = a;
weight = b;
}
void vehicle::show()
{
cout << "车轮数:" << wheels << endl;
cout << "重量:" << weight << endl;
}
class car :public vehicle
{
int passenger;
public:
car(int wheels1, double weight1, int passenger1);
void show();
};
car::car(int wheels1, double weight1, int passenger1):vehicle(wheels1, weight1)
{
passenger = passenger1;
}
void car::show()
{
cout << "小车类:" << endl;
vehicle::show();
cout << "载人数:" << passenger << endl;
}
class truck :public vehicle
{
int passenger;
double payload;
public:
truck(int wheels1, double weight1, int passenger1,double payload1);
void show();
};
truck::truck(int wheels1, double weight1, int passenger1, double payload1):vehicle(wheels1, weight1)
{
passenger = passenger1;
payload = payload1;
}
void truck::show()
{
cout << "卡车类:" << endl;
vehicle::show();
cout << "载人数:" << passenger << endl;
cout << "载重量:" << payload << endl;
}
int main()
{
car a(4, 100, 5);
truck b(6, 500, 2, 300);
a.show();
cout << endl;
b.show();
system("PAUSE");
return 0;
}