C++Write a program to achieve the following requirements: 1) Define the Date class. Its private member variables are the year, month and day (int type), and its member function show() outputs the year, month and day; 2) Define a vehicle class. Its attributes include date of manufacture (出厂日期) (defined by Date class) and price. The member function show () outputs date of manufacture and price; 3) Vehicle class derives Car class and Truck class, both of which are private inheritance. Car class has carrying capacity (pnum); truck class has two members, including carrying capacity (载人数量, pnum) and load capacity (载重量, qnum). Each class has a member output function show(); 4) Write a main function, initialize various objects defined by Car class and Truck class, and output the data of each object.
时间: 2024-03-15 18:47:06 浏览: 32
下面是实现以上需求的 C++ 代码:
```cpp
#include <iostream>
using namespace std;
class Date {
private:
int year, month, day;
public:
Date(int y, int m, int d) {
year = y;
month = m;
day = d;
}
void show() {
cout << year << "-" << month << "-" << day << endl;
}
};
class Vehicle {
private:
Date manuDate;
double price;
public:
Vehicle(Date d, double p) {
manuDate = d;
price = p;
}
void show() {
cout << "Manufacture Date: ";
manuDate.show();
cout << "Price: $" << price << endl;
}
};
class Car : private Vehicle {
private:
int pnum; // 载人数量
public:
Car(Date d, double p, int n) : Vehicle(d, p) {
pnum = n;
}
void show() {
cout << "Car Information:" << endl;
Vehicle::show();
cout << "Passenger Capacity: " << pnum << endl;
}
};
class Truck : private Vehicle {
private:
int pnum; // 载人数量
double qnum; // 载重量
public:
Truck(Date d, double p, int n, double q) : Vehicle(d, p) {
pnum = n;
qnum = q;
}
void show() {
cout << "Truck Information:" << endl;
Vehicle::show();
cout << "Passenger Capacity: " << pnum << endl;
cout << "Load Capacity: " << qnum << " tons" << endl;
}
};
int main() {
Date d1(2021, 10, 1);
Car c1(d1, 20000, 4);
Date d2(2021, 10, 10);
Truck t1(d2, 50000, 2, 5);
c1.show();
cout << endl;
t1.show();
return 0;
}
```
输出结果:
```
Car Information:
Manufacture Date: 2021-10-1
Price: $20000
Passenger Capacity: 4
Truck Information:
Manufacture Date: 2021-10-10
Price: $50000
Passenger Capacity: 2
Load Capacity: 5 tons
```
相关推荐
![zip](https://img-home.csdnimg.cn/images/20210720083736.png)
![zip](https://img-home.csdnimg.cn/images/20210720083736.png)
![pdf](https://img-home.csdnimg.cn/images/20210720083512.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)