设计Flight类。 要求: (1)具有机型、班次、额定载客数和实际载客数等属性; (2)具有输入/输出属性功能; (3)求载客率。
时间: 2024-03-25 13:38:50 浏览: 59
好的,以下是Flight类的C++代码:
```c++
#include <iostream>
using namespace std;
class Flight {
private:
string aircraft_type;
string flight_number;
int passenger_capacity;
int actual_passenger_number;
public:
void input_attributes() {
cout << "请输入机型:";
cin >> aircraft_type;
cout << "请输入班次:";
cin >> flight_number;
cout << "请输入额定载客数:";
cin >> passenger_capacity;
cout << "请输入实际载客数:";
cin >> actual_passenger_number;
}
void output_attributes() {
cout << "机型:" << aircraft_type << endl;
cout << "班次:" << flight_number << endl;
cout << "额定载客数:" << passenger_capacity << endl;
cout << "实际载客数:" << actual_passenger_number << endl;
}
double passenger_load_factor() {
return (double)actual_passenger_number / (double)passenger_capacity;
}
};
int main() {
Flight flight;
flight.input_attributes();
cout << endl;
flight.output_attributes();
double load_factor = flight.passenger_load_factor();
cout << "载客率:" << load_factor << endl;
return 0;
}
```
Flight类有四个私有属性:机型(aircraft_type)、班次(flight_number)、额定载客数(passenger_capacity)和实际载客数(actual_passenger_number)。类中有三个公有方法:input_attributes()、output_attributes()和passenger_load_factor()。
input_attributes()方法用于输入对象的属性值,通过cin获取用户输入的值,然后将它们分别赋值给实例变量。
output_attributes()方法用于输出对象的属性值,通过cout将对象的属性值一一输出。
passenger_load_factor()方法用于计算载客率,返回实际载客数与额定载客数的比值。
在main函数中,首先创建一个Flight类的对象flight,然后调用input_attributes()方法输入对象的属性值,再调用output_attributes()方法输出对象的属性值。最后调用passenger_load_factor()方法计算载客率,并输出结果。
阅读全文