编写一个求出租车收费的程序,输入起始站、终止站和路程。 计费标准为:3 公里以内10元,3 公里以后每1公里加 2 元,超过 15 公里,每公里加 3元。 要求:设计一个站类Station(用于设置起始站、终止站)和路程类Mile(用于设置路程),由这两个类派生出收费类Price(用于计费)。 已知Station类和Mile类以及测试main函数如下,请编写Price类。 (可以使用string类型代替给出代码中的字符数组。)
时间: 2023-06-09 07:08:54 浏览: 74
以下是根据您的要求所编写的程序:
#include<iostream>
#include<cstring>
using namespace std;
class Station
{
protected:
string start_station;
string end_station;
public:
Station(string s1 = "None", string s2 = "None") : start_station(s1), end_station(s2) {}
void setStation(string s1, string s2)
{
start_station = s1;
end_station = s2;
}
};
class Mile
{
protected:
int distance;
public:
Mile(int dis = 0) : distance(dis) {}
void setDistance(int dis)
{
distance = dis;
}
};
class Price : public Station, public Mile
{
public:
Price(string s1 = "None", string s2 = "None", int dis = 0) : Station(s1, s2), Mile(dis) {}
double getPrice()
{
double price = 0;
if (distance <= 3)
{
price = 10;
}
else if (distance <= 15)
{
price = 10 + (distance - 3) * 2;
}
else
{
price = 10 + 12 * 2 + (distance - 15) * 3;
}
return price;
}
};
int main()
{
string start, end;
int dis;
cout << "请输入起始站:" << endl;
cin >> start;
cout << "请输入终点站:" << endl;
cin >> end;
cout << "请输入路程(公里):" << endl;
cin >> dis;
Price p(start, end, dis);
cout << "价格为:" << p.getPrice() << "元" << endl;
return 0;
}
注意:本程序仅限用于学习交流,请勿用于商业用途。