【C++练习】设计一个汽车类vehicle

  1. 设计一个汽车类vehicle,数据成员包括:车轮个数、车重。小车类car是它的公有派生类,新增数据成员:载人数passenger;卡车truck是汽车类的公有派生类,新增数据成员:载人数passenger和载重量:payload,另外每个类都有相关数据输出方法,其他数据和方法可以自行添加,请设计实现这三个类,并编写测试代码进行测试。
/*
设计一个汽车类vehicle,数据成员包括:车轮个数、车重。
小车类car是它的公有派生类,新增数据成员:载人数passenger;
卡车truck是汽车类的公有派生类,新增数据成员:载人数passenger和载重量:payload,另外每个类都有相关数据输出方法
*/
#include 
#include 
#include 
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;
}

你可能感兴趣的:(C/C++,C++练习)