请用c++实现创建Package类继承层次结构,创建了一个用于显示若干Package的地址信息并计算其运输费的程序。程序中包含一个Package指针的对组,可设数组大小为3,分别存放指向Package、TwoDayPackage和OvernightPackage对象的三个指针,可在创建对象的同时赋初值。初值如下: // initialize vector with Packages packages[ 0 ] = new Package( "Lou Brown", "1 Main St", "Boston", "MA", 11111, "Mary Smith", "7 Elm St", "New York", "NY", 22222, 8.5, .5 ); packages[ 1 ] = new TwoDayPackage( "Lisa Klein", "5 Broadway", "Somerville", "MA", 33333, "Bob George", "21 Pine Rd", "Cambridge", "MA", 44444, 10.4, .65, 2.0 ); packages[ 2 ] = new OvernightPackage( "Ed Lewis", "2 Oak St", "Boston", "MA", 55555, "Don Kelly", "9 Main St", "Denver", "CO", 66666, 12.25, .7, .25 ); 遍历这个指针数组,多态性的处理这些Package。对于每个Package调用获取函数,获得发送者和接受者的地址信息,然后打印输出这两个地址,此外,调用每个Package的CalculateCost成员函数并输出结果。最后显示所有Package的总的运输费用。
时间: 2024-01-05 17:11:13 浏览: 91
Experiment 1_C++_测绘程序_方位角计算_
5星 · 资源好评率100%
好的,下面是C++的代码实现:
```
#include <iostream>
#include <string>
#include <vector>
using namespace std;
class Package {
public:
Package(string sName, string sAddress, string sCity, string sState, int sZip,
string rName, string rAddress, string rCity, string rState, int rZip,
double w, double c) {
senderName = sName;
senderAddress = sAddress;
senderCity = sCity;
senderState = sState;
senderZip = sZip;
receiverName = rName;
receiverAddress = rAddress;
receiverCity = rCity;
receiverState = rState;
receiverZip = rZip;
weight = w;
costPerOunce = c;
}
virtual double CalculateCost() {
return weight * costPerOunce;
}
string GetSenderAddress() {
return senderAddress + ", " + senderCity + ", " + senderState + ", " + to_string(senderZip);
}
string GetReceiverAddress() {
return receiverAddress + ", " + receiverCity + ", " + receiverState + ", " + to_string(receiverZip);
}
protected:
string senderName;
string senderAddress;
string senderCity;
string senderState;
int senderZip;
string receiverName;
string receiverAddress;
string receiverCity;
string receiverState;
int receiverZip;
double weight;
double costPerOunce;
};
class TwoDayPackage : public Package {
public:
TwoDayPackage(string sName, string sAddress, string sCity, string sState, int sZip,
string rName, string rAddress, string rCity, string rState, int rZip,
double w, double c, double f) : Package(sName, sAddress, sCity, sState, sZip, rName, rAddress, rCity, rState, rZip, w, c) {
flatFee = f;
}
double CalculateCost() {
return Package::CalculateCost() + flatFee;
}
protected:
double flatFee;
};
class OvernightPackage : public Package {
public:
OvernightPackage(string sName, string sAddress, string sCity, string sState, int sZip,
string rName, string rAddress, string rCity, string rState, int rZip,
double w, double c, double f) : Package(sName, sAddress, sCity, sState, sZip, rName, rAddress, rCity, rState, rZip, w, c) {
overnightFee = f;
}
double CalculateCost() {
return Package::CalculateCost() + weight * overnightFee;
}
protected:
double overnightFee;
};
int main() {
vector<Package*> packages(3);
packages[0] = new Package("Lou Brown", "1 Main St", "Boston", "MA", 11111, "Mary Smith", "7 Elm St", "New York", "NY", 22222, 8.5, .5);
packages[1] = new TwoDayPackage("Lisa Klein", "5 Broadway", "Somerville", "MA", 33333, "Bob George", "21 Pine Rd", "Cambridge", "MA", 44444, 10.4, .65, 2.0);
packages[2] = new OvernightPackage("Ed Lewis", "2 Oak St", "Boston", "MA", 55555, "Don Kelly", "9 Main St", "Denver", "CO", 66666, 12.25, .7, .25);
double totalCost = 0.0;
for (const auto& pkg : packages) {
cout << "Sender Address: " << pkg->GetSenderAddress() << endl;
cout << "Receiver Address: " << pkg->GetReceiverAddress() << endl;
double cost = pkg->CalculateCost();
totalCost += cost;
cout << "Package cost: $" << cost << endl;
cout << endl;
}
cout << "Total cost for all packages: $" << totalCost << endl;
for (auto& pkg : packages) {
delete pkg;
}
return 0;
}
```
此程序定义了一个`Package`类及其两个派生类`TwoDayPackage`和`OvernightPackage`,分别代表普通包裹和两日包裹、隔夜包裹。每个类都有一个`CalculateCost()`成员函数,可以计算运输费用;另外,`Package`类还提供了获取发送者和接收者地址信息的成员函数。
程序中使用了一个指针数组,存储了三个指向不同类型的包裹对象的指针。在遍历数组时,使用了多态性,即通过基类指针调用派生类函数,从而实现了不同类型包裹的计算和输出。
阅读全文