使用c++ 定义一个快递包裹类 Package,并由此派生出普通包裹类和加急包裹类。 要求: (1)Package 类应包含寄件人和收件人姓名、地址和包裹重量属性,按重量*10 计费。 (2)普通包裹增加首重计费和超首重后部分的的每公斤计费单价属性,按首重+超重*每公斤计费单价计算总费用。
时间: 2024-02-24 16:59:54 浏览: 124
好的,我来回答你的问题。以下是根据你的要求定义的 C++ 代码:
```c++
#include <string>
#include <iostream>
using namespace std;
class Package {
public:
Package(string senderName, string senderAddress, string receiverName, string receiverAddress, double weight);
virtual ~Package() {}
virtual double calculateCost() const;
protected:
string m_senderName;
string m_senderAddress;
string m_receiverName;
string m_receiverAddress;
double m_weight;
};
Package::Package(string senderName, string senderAddress, string receiverName, string receiverAddress, double weight)
: m_senderName(senderName)
, m_senderAddress(senderAddress)
, m_receiverName(receiverName)
, m_receiverAddress(receiverAddress)
, m_weight(weight) {}
double Package::calculateCost() const {
return m_weight * 10.0;
}
class RegularPackage : public Package {
public:
RegularPackage(string senderName, string senderAddress, string receiverName, string receiverAddress, double weight, double firstWeightPrice, double additionalWeightPrice);
virtual double calculateCost() const override;
private:
double m_firstWeightPrice;
double m_additionalWeightPrice;
};
RegularPackage::RegularPackage(string senderName, string senderAddress, string receiverName, string receiverAddress, double weight, double firstWeightPrice, double additionalWeightPrice)
: Package(senderName, senderAddress, receiverName, receiverAddress, weight)
, m_firstWeightPrice(firstWeightPrice)
, m_additionalWeightPrice(additionalWeightPrice) {}
double RegularPackage::calculateCost() const {
if (m_weight <= 1) {
return m_firstWeightPrice;
} else {
return m_firstWeightPrice + (m_weight - 1) * m_additionalWeightPrice;
}
}
class ExpeditedPackage : public Package {
public:
ExpeditedPackage(string senderName, string senderAddress, string receiverName, string receiverAddress, double weight, double expeditedPrice);
virtual double calculateCost() const override;
private:
double m_expeditedPrice;
};
ExpeditedPackage::ExpeditedPackage(string senderName, string senderAddress, string receiverName, string receiverAddress, double weight, double expeditedPrice)
: Package(senderName, senderAddress, receiverName, receiverAddress, weight)
, m_expeditedPrice(expeditedPrice) {}
double ExpeditedPackage::calculateCost() const {
return Package::calculateCost() + m_expeditedPrice;
}
int main() {
// 创建一个普通包裹
RegularPackage regularPackage("John", "123 Main St.", "Mary", "456 Broad St.", 2.5, 5.0, 2.0);
cout << "Regular Package Cost: $" << regularPackage.calculateCost() << endl;
// 创建一个加急包裹
ExpeditedPackage expeditedPackage("John", "123 Main St.", "Mary", "456 Broad St.", 2.5, 10.0);
cout << "Expedited Package Cost: $" << expeditedPackage.calculateCost() << endl;
return 0;
}
```
在这个代码中,我们首先定义了一个基类 Package,包含了寄件人和收件人姓名、地址和包裹重量属性,并且实现了一个 calculateCost() 函数来计算该包裹的费用。然后,我们派生出了普通包裹类 RegularPackage 和加急包裹类 ExpeditedPackage。普通包裹类增加了首重计费和超首重后部分的的每公斤计费单价属性,并重写了 calculateCost() 函数来计算总费用。加急包裹类则增加了一个加急费用属性,并同样重写了 calculateCost() 函数来计算总费用。在 main() 函数中,我们创建了一个普通包裹和一个加急包裹,并分别输出它们的总费用。
阅读全文