12.13(Package继承层次)使用在练习题11.9中创建的Package类继承层次结构,创建一个用于显示若干Package的地址信息并计算其运输费用的程序。程序应该包含一个Package 指针的 vector 对象,其中的指针指向TwoDayPackage 对象和OvernightPackage对象。遍历该vector对象,多态性地处理这些Package。对于每个Package调用get函数,获得发送者和接收者的地址信息,然后打印输出这两个地址,就像它们出现在邮包标签上一样。此外,调用每个Package的calculateCost成员函数并输出结果。跟踪记录该vector中所有Package的总的运输费用,并在循环遍历结束时显示此总费用。
时间: 2024-03-09 15:46:38 浏览: 59
好的,让我来解答你的问题。
针对你的问题,我可以回答:好的,我们可以使用在练习题11.9中创建的Package类继承层次结构,创建一个用于显示若干Package的地址信息并计算其运输费用的程序。
以下是实现该程序的步骤:
1. 引入头文件和命名空间:
```cpp
#include <iostream>
#include <vector>
#include "Package.h"
#include "TwoDayPackage.h"
#include "OvernightPackage.h"
using namespace std;
```
2. 创建一个指向Package对象的指针的vector对象:
```cpp
vector<Package*> packages;
```
3. 创建两个TwoDayPackage对象和两个OvernightPackage对象,并将它们的指针加入到packages中:
```cpp
TwoDayPackage t1("sender1", "receiver1", "address1", "city1", "state1", "123456", "address2", "city2", "state2", "654321", 10.0, 2.0, 1.0);
TwoDayPackage t2("sender2", "receiver2", "address3", "city3", "state3", "789456", "address4", "city4", "state4", "456123", 5.0, 3.0, 1.5);
OvernightPackage o1("sender3", "receiver3", "address5", "city5", "state5", "987654", "address6", "city6", "state6", "321654", 8.0, 2.0, 1.5);
OvernightPackage o2("sender4", "receiver4", "address7", "city7", "state7", "456789", "address8", "city8", "state8", "789123", 12.0, 2.5, 2.0);
packages.push_back(&t1);
packages.push_back(&t2);
packages.push_back(&o1);
packages.push_back(&o2);
```
4. 遍历packages中的每个Package对象,多态性地处理这些Package,并输出其地址信息和运输费用:
```cpp
double totalCost = 0.0;
for (Package* p : packages) {
cout << "Sender's address: " << p->getSenderAddress() << endl;
cout << "Receiver's address: " << p->getReceiverAddress() << endl;
double cost = p->calculateCost();
cout << "Shipping cost: " << cost << endl;
totalCost += cost;
}
cout << "Total shipping cost: " << totalCost << endl;
```
完整的程序如下所示:
```cpp
#include <iostream>
#include <vector>
#include "Package.h"
#include "TwoDayPackage.h"
#include "OvernightPackage.h"
using namespace std;
int main() {
vector<Package*> packages;
TwoDayPackage t1("sender1", "receiver1", "address1", "city1", "state1", "123456", "address2", "city2", "state2", "654321", 10.0, 2.0, 1.0);
TwoDayPackage t2("sender2", "receiver2", "address3", "city3", "state3", "789456", "address4", "city4", "state4", "456123", 5.0, 3.0, 1.5);
OvernightPackage o1("sender3", "receiver3", "address5", "city5", "state5", "987654", "address6", "city6", "state6", "321654", 8.0, 2.0, 1.5);
OvernightPackage o2("sender4", "receiver4", "address7", "city7", "state7", "456789", "address8", "city8", "state8", "789123", 12.0, 2.5, 2.0);
packages.push_back(&t1);
packages.push_back(&t2);
packages.push_back(&o1);
packages.push_back(&o2);
double totalCost = 0.0;
for (Package* p : packages) {
cout << "Sender's address: " << p->getSenderAddress() << endl;
cout << "Receiver's address: " << p->getReceiverAddress() << endl;
double cost = p->calculateCost();
cout << "Shipping cost: " << cost << endl;
totalCost += cost;
}
cout << "Total shipping cost: " << totalCost << endl;
return 0;
}
```
希望我的回答能够帮到你。
阅读全文