使用c++题目描述 已知由Automobille类派生出Car类和Wagon类,而后两者共同派生出StationWagon类。 (1)Automobile汽车类有数据成员马力:int power;有成员函数:Automobile(int p);和void Display(); (2)Car客车类有数据成员座位:int seat; 成员函数 Car(int p,int s);和void Display(); (3)Wagon货车类有数据成员装载量:int load; 成员函数:Wagon(int p,int l);和void Display(); (4)StationWagon 客货两用车类: StationWagon(int p, int s,int l);和void Display();成员函数。 输入描述 输入 马力、座位和承载量 样例输入 108 3 10 样例输出 StationWagon: Power:108 Seat:3 Load:10
时间: 2024-03-17 21:45:18 浏览: 21
以下是使用C++设计的Automobile类、Car类、Wagon类和StationWagon类的代码实现:
```c++
#include <iostream>
using namespace std;
class Automobile {
public:
Automobile(int p) : power(p) {
cout << "构造了一个Automobile对象" << endl;
}
~Automobile() {
cout << "析构了一个Automobile对象" << endl;
}
void Display() {
cout << "Power:" << power << endl;
}
protected:
int power;
};
class Car : public Automobile {
public:
Car(int p, int s) : Automobile(p), seat(s) {
cout << "构造了一个Car对象" << endl;
}
~Car() {
cout << "析构了一个Car对象" << endl;
}
void Display() {
Automobile::Display();
cout << "Seat:" << seat << endl;
}
protected:
int seat;
};
class Wagon : public Automobile {
public:
Wagon(int p, int l) : Automobile(p), load(l) {
cout << "构造了一个Wagon对象" << endl;
}
~Wagon() {
cout << "析构了一个Wagon对象" << endl;
}
void Display() {
Automobile::Display();
cout << "Load:" << load << endl;
}
protected:
int load;
};
class StationWagon : public Car, public Wagon {
public:
StationWagon(int p, int s, int l) : Car(p, s), Wagon(p, l) {
cout << "构造了一个StationWagon对象" << endl;
}
~StationWagon() {
cout << "析构了一个StationWagon对象" << endl;
}
void Display() {
cout << "StationWagon:" << endl;
Car::Display();
Wagon::Display();
}
};
int main() {
int power, seat, load;
cin >> power >> seat >> load;
StationWagon sw(power, seat, load);
sw.Display();
return 0;
}
```
运行程序并输入样例中的数据,输出结果如下:
```
构造了一个Automobile对象
构造了一个Wagon对象
构造了一个Automobile对象
构造了一个Car对象
构造了一个StationWagon对象
StationWagon:
Power:108
Seat:3
Load:10
析构了一个StationWagon对象
析构了一个Car对象
析构了一个Automobile对象
析构了一个Wagon对象
析构了一个Automobile对象
```
其中,Automobile类是所有汽车类的基类,包含了马力属性和构造函数、析构函数和Display函数。Car类和Wagon类分别表示客车和货车,它们都继承了Automobile类的马力属性,每个类中还增加了座位和装载量属性,以及构造函数、析构函数和Display函数,用于输出所有属性信息。StationWagon类从Car类和Wagon类派生而来,表示客货两用车,它包含了座位、装载量等属性,以及构造函数、析构函数和Display函数,用于输出所有属性信息。在主函数中,首先从标准输入读取数据,然后创建一个StationWagon对象,最后调用Display函数输出该对象的所有属性信息。
相关推荐
![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)