设计类 vehicle。 a.数据成员包括: 交通工具名称、重量、座位数、最高时速;(交通工具名称用string类型存储)。 b.成员函数及其功能: 设置交通工具名称的函数setname(stringn);设置重量的函数sctweight(intw);*设置座位数的函数setsite(ints);2 设置最高时速的函数sethighspeed(int hs); 计算最高时速时拥有的动量的函数momentumO;。输出交通工具所有信息的函数output; c.编写主函数,主函数中分别输入交通工具名称、重量、座位数、最高时速;定义对象,通过对象访问相应的set函数输入信息,再调用output函数输出信息。(要输出的信息包括:交通工具名称、座位数、最高时速以及最高时速的动量,最高时速动量-重量*最该时速,运行效果如下图所示)。
时间: 2024-03-20 10:40:17 浏览: 27
以下是类 vehicle 的实现代码:
```cpp
#include <iostream>
#include <string>
using namespace std;
class vehicle {
private:
string name;
int weight;
int seats;
int highspeed;
public:
void setname(string n) {
name = n;
}
void setweight(int w) {
weight = w;
}
void setseats(int s) {
seats = s;
}
void sethighspeed(int hs) {
highspeed = hs;
}
int momentum() {
return highspeed * weight;
}
void output() {
cout << "交通工具名称:" << name << endl;
cout << "座位数:" << seats << endl;
cout << "最高时速:" << highspeed << " km/h" << endl;
cout << "最高时速动量:" << momentum() << " kg·km/h" << endl;
}
};
int main() {
vehicle v;
string name;
int weight, seats, highspeed;
cout << "请输入交通工具名称:";
cin >> name;
v.setname(name);
cout << "请输入重量(kg):";
cin >> weight;
v.setweight(weight);
cout << "请输入座位数:";
cin >> seats;
v.setseats(seats);
cout << "请输入最高时速(km/h):";
cin >> highspeed;
v.sethighspeed(highspeed);
v.output();
return 0;
}
```
运行效果如下:
```
请输入交通工具名称:汽车
请输入重量(kg):1500
请输入座位数:5
请输入最高时速(km/h):120
交通工具名称:汽车
座位数:5
最高时速:120 km/h
最高时速动量:180000 kg·km/h
```
相关推荐
![pptx](https://img-home.csdnimg.cn/images/20210720083543.png)
![rar](https://img-home.csdnimg.cn/images/20210720083606.png)
![-](https://csdnimg.cn/download_wenku/file_type_lunwen.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)