设计程序实现轿车和货车信息输入和打印功能。轿车信息有发动机、车轮数、车重、车宽、车长、车高;货车信息有发动机、车轮数、车重、吨位级别(字符类型)、货箱类型(字符类型)、货箱长、货箱宽、货箱高。其中: 吨位级别:A代表轻卡,B代表中卡,C代表重卡; 货箱类型:A代表栏板式,B代表仓栅式; 发动机信息由发动机型号(字符串)表示。 设计要求: (1)根据轿车和货车的描述,抽取其共同特征定义为机动车类。完成机动车类的定义,并编写该类的构造函数、信息输入函数及信息输出函数。从机动车类派生轿车类,完成该类定义并编写其构造函数、信息输入函数及输出函数(3)从机动车类派生货车类,完成该类定义并编写其构造函数、信息输入函数及输出函数。(10分) (4)编写主程序,创建轿车类对象和货车类对象,实现轿车类对象和货车类对象信息输入和输出。c++实现
时间: 2024-02-27 11:58:22 浏览: 30
好的,以下是C++实现:
```cpp
#include <iostream>
#include <string>
using namespace std;
class MotorVehicle { // 机动车类
protected:
string engine; // 发动机型号
int wheels; // 车轮数
double weight, width, length, height; // 车重、车宽、车长、车高
public:
MotorVehicle(string e = "", int w = 0, double wt = 0, double wd = 0, double l = 0, double h = 0)
: engine(e), wheels(w), weight(wt), width(wd), length(l), height(h) {}
void input() { // 信息输入函数
cout << "请输入发动机型号:";
getline(cin, engine);
cout << "请输入车轮数:";
cin >> wheels;
cout << "请输入车重、车宽、车长、车高(用空格分隔):";
cin >> weight >> width >> length >> height;
}
void output() { // 信息输出函数
cout << "发动机型号:" << engine << endl;
cout << "车轮数:" << wheels << endl;
cout << "车重:" << weight << " 吨" << endl;
cout << "车宽:" << width << " 米" << endl;
cout << "车长:" << length << " 米" << endl;
cout << "车高:" << height << " 米" << endl;
}
};
class Car : public MotorVehicle { // 轿车类
public:
Car(string e = "", int w = 0, double wt = 0, double wd = 0, double l = 0, double h = 0)
: MotorVehicle(e, w, wt, wd, l, h) {}
void input() { // 信息输入函数
MotorVehicle::input(); // 调用基类的信息输入函数
}
void output() { // 信息输出函数
cout << "车辆类型:轿车" << endl;
MotorVehicle::output(); // 调用基类的信息输出函数
}
};
class Truck : public MotorVehicle { // 货车类
protected:
char tonnage; // 吨位级别,A代表轻卡,B代表中卡,C代表重卡
char boxType; // 货箱类型,A代表栏板式,B代表仓栅式
double boxLength, boxWidth, boxHeight; // 货箱长、货箱宽、货箱高
public:
Truck(string e = "", int w = 0, double wt = 0, char t = 'A', char b = 'A', double bl = 0, double bw = 0, double bh = 0)
: MotorVehicle(e, w, wt), tonnage(t), boxType(b), boxLength(bl), boxWidth(bw), boxHeight(bh) {}
void input() { // 信息输入函数
MotorVehicle::input(); // 调用基类的信息输入函数
cout << "请输入吨位级别(A代表轻卡,B代表中卡,C代表重卡):";
cin >> tonnage;
cout << "请输入货箱类型(A代表栏板式,B代表仓栅式):";
cin >> boxType;
cout << "请输入货箱长、货箱宽、货箱高(用空格分隔):";
cin >> boxLength >> boxWidth >> boxHeight;
}
void output() { // 信息输出函数
cout << "车辆类型:货车" << endl;
MotorVehicle::output(); // 调用基类的信息输出函数
cout << "吨位级别:" << tonnage << endl;
cout << "货箱类型:" << boxType << endl;
cout << "货箱长:" << boxLength << " 米" << endl;
cout << "货箱宽:" << boxWidth << " 米" << endl;
cout << "货箱高:" << boxHeight << " 米" << endl;
}
};
int main() {
Car myCar;
Truck myTruck;
cout << "请输入轿车信息:" << endl;
myCar.input();
cout << "请输入货车信息:" << endl;
myTruck.input();
cout << "轿车信息如下:" << endl;
myCar.output();
cout << endl << "货车信息如下:" << endl;
myTruck.output();
return 0;
}
```
以上程序定义了三个类:`MotorVehicle`(机动车类)、`Car`(轿车类)和`Truck`(货车类),其中`Car`和`Truck`都是从`MotorVehicle`派生而来的子类。每个类中都有构造函数、信息输入函数和信息输出函数。主程序中创建了一个轿车对象和一个货车对象,并分别调用它们的信息输入函数和信息输出函数。
相关推荐
![zip](https://img-home.csdnimg.cn/images/20210720083736.png)
![zip](https://img-home.csdnimg.cn/images/20210720083736.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)