汽车类 vehicle

140 阅读1分钟

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

#include <iostream>
#include <cstdlib>
using namespace std;

class vehicle //汽车类
{
protected:
	int wheels; // 车轮数
	double weight; // 重量
public:
	vehicle(int a,double b);

    __attribute__((unused)) int GetWheels() const
    {
        return wheels;
    }

    __attribute__((unused)) double GetWeight() const
    {
        return weight;
    }

    virtual 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() override;
};
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() override;
};
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()
{
    system("chcp 65001 > null");
    int car_wheels , car_passenger ;
    double car_weight ;
    cout << "输入汽车的车轮数、重量、载人数:" << endl ;
    cin >> car_wheels >> car_weight >> car_passenger ;
    int struck_wheels , struck_passenger ;
    double struck_weight , struck_payload ;
    cout << "输入卡车的车轮数、重量、载人数、载重量:" << endl ;
    cin >> struck_wheels >> struck_weight >> struck_passenger >> struck_payload ;
	car a(car_wheels, car_weight, car_passenger);
	truck b(struck_wheels, struck_weight, struck_passenger, struck_payload);
	a.show();
	cout << endl;
	b.show();
	system("PAUSE");
	return 0;
}