创建 Package类
【问题描述】
- 一些快递商,如 FedEx、DHL和UPS,都提供多样化的服务,同时也收取不同的费用。创建一个表示各种不同包裹的继承层次。以包裹类Package作为基类,两日包裹类TwoDayPackage和连夜包裹类OvernightPackage作为派生类。
- 基类Package应该包括含代表寄件人和收件人姓名、地址、所在城市、所在州和邮政编码等的数据成员。此外,还应包含存储包裹重量(以盎司计)和每盎司费用的数据成员。类的构造函数应初始化这些数据成员,并确保重量和每盎司费用为正值。Package应该提供public成员函数calculateCost,该函数计算重量和每盎司费用的乘积,得到的是与运输该包裹有关的费用并返回(返回值类型为double)。
- 派生类TwDayPackage应继承基类Package的功能,但还应包含一个数据成员,表示给两日快递服务的平寄费。TwoDayPackage构造函数应接受一个值来初始化这个数据成员。类TwoDayPackage还应该重新定义基类的成员函数calculateCost计输费用,具体方法将平寄费加到基类Package的 calculateCost函数计算得到的基于重量的费用中。
- 派生类OvenightPackage应直接继承基类,并且应包含一个附加的数据成员,表示付给连夜快递服务的每盎司的额外费用。类 OvernightPackage应当重新定义基类的成员函数calculateCost来使它在计算运输费用之前,先将额外每盎司费用加到标准的每盎司费用上。
- 编写测试程序,创建每种Package的对象并测试成员函数 calculateCost。
【相关的解决方法】
1.编写基类Package
class Package
{
public://声明为常量引用,使用引用避免copy,使用const能保护数据
Package(const string&, const string&, const string&, const string&,const string&, int , double , double);
double calculateCost() const;
double getWeight() const;
double getCostPerOunce() const;
private:
string senderName;
string recipientName;
string address;
string city;
string state;
int postalCode;
double weight;
double costPerOunce;//Cost per ounce每一盎司费用 ;盎司:英美制单位,既是重量单位又是容量单位
};
2.两日包裹类TwoDayPackage,继承于Package类
class TwoDayPackage : public Package
{
public:
TwoDayPackage(const string&, const string&, const string&, const string&,const string&, int , double , double, double);
double calculateCost() const;//基类的成员函数需要在派生类内声明才可被使用
private:
double surfaceMailCosts;//平寄费
};
3.连夜包裹类OvernightPackage,继承于Package类
class OvernightPackage : public Package
{
public:
OvernightPackage(const string&, const string&, const string&, const string&,const string&, int , double , double, double);
double calculateCost() const;
private:
double extraCosts;//连夜快递服务的额外费用
};
4.Package类的构造函数初始化数据成员,并确保重量和每盎司费用为正值,提供public成员函数calculateCost,该函数计算重量和每盎司费用的乘积,得到的是与运输该包裹有关的费用并返回(返回值类型为double)。
Package::Package(const string& senderName1,const string& recipientName1, const string& address1, const string& city1,const string& state1,int postalCode1, double weight1, double costPerOunce1)
: senderName(senderName1),recipientName(recipientName1),address(address1),city(city1),state(state1),postalCode(postalCode1),weight(weight1),costPerOunce(costPerOunce1)
{
if(weight1>0)
{
weight=weight1;
}
else
{
weight=1;
cout << "Weight less than 0 !" << endl;
}
}
double Package::calculateCost() const
{
double transCosts;
transCosts=weight*costPerOunce;
return transCosts;
}
double Package::getWeight() const
{
return weight;
}
double Package::getCostPerOunce() const
{
return costPerOunce;
}
5.TwoDayPackage构造函数接受一个值来初始化数据成员。并重新定义基类的成员函数calculateCost来计输费用,具体方法将平寄费加到基类Package的 calculateCost函数计算得到的基于重量的费用中。
TwoDayPackage::TwoDayPackage(const string& senderName1,const string& recipientName1, const string& address1, const string& city1,const string& state1,int postalCode1, double weight1, double costPerOunce1,double surfaceMailCosts1)
:Package(senderName1,recipientName1,address1,city1,state1,postalCode1,weight1,costPerOunce1)
{
surfaceMailCosts=surfaceMailCosts1;
}
double TwoDayPackage::calculateCost() const
{
double transCosts;
transCosts = Package::calculateCost() + surfaceMailCosts;
return transCosts;
}
6.类 OvernightPackage重新定义基类的成员函数calculateCost来使它在计算运输费用之前,先将额外每盎司费用加到标准的每盎司费用上。
OvernightPackage::OvernightPackage(const string& senderName1,const string& recipientName1, const string& address1, const string& city1,const string& state1,int postalCode1, double weight1, double costPerOunce1,double extraCosts1)
:Package(senderName1,recipientName1,address1,city1,state1,postalCode1,weight1,costPerOunce1)
{
extraCosts=extraCosts1;
}
double OvernightPackage::calculateCost() const
{
double transCosts;
transCosts = Package::calculateCost() + extraCosts;
return transCosts;
}
7.编写测试程序,创建每种Package的对象并测试成员函数 calculateCost。
//测试程序
int main()
{
Package text1("Lihua","Kandy","America","NewYork","NewYorkState",10041,30,5) ;
cout << "The cost of a regular package is :" << text1.calculateCost() << endl;
cout << endl;
TwoDayPackage text2("Zhang shan","Kandy","America","Houston","Texas",77001,50,8,25.6);
cout << "The cost of two-day package is : " << text2.calculateCost() << endl;
cout << endl;
OvernightPackage text3("","Kandy","America","St. Paul","Minnesota",35350,25,4,36.5);
cout << "The cost of overnight package is : " << text3.calculateCost() << endl;
cout << endl;
return 0;
}
完整代码如下:
/
//Package继承层次.cpp
/
#include
#include
using namespace std;
class Package
{
public://声明为常量引用,使用引用避免copy,使用const能保护数据
Package(const string&, const string&, const string&, const string&,const string&, int , double , double);
double calculateCost() const;
double getWeight() const;
double getCostPerOunce() const;
private:
string senderName;
string recipientName;
string address;
string city;
string state;
int postalCode;
double weight;
double costPerOunce;//Cost per ounce每一盎司费用 ;盎司:英美制单位,既是重量单位又是容量单位
};
class TwoDayPackage : public Package
{
public:
TwoDayPackage(const string&, const string&, const string&, const string&,const string&, int , double , double, double);
double calculateCost() const;//基类的成员函数需要在派生类内声明才可被使用
private:
double surfaceMailCosts;//平寄费
};
class OvernightPackage : public Package
{
public:
OvernightPackage(const string&, const string&, const string&, const string&,const string&, int , double , double, double);
double calculateCost() const;
private:
double extraCosts;//连夜快递服务的额外费用
};
Package::Package(const string& senderName1,const string& recipientName1, const string& address1, const string& city1,const string& state1,int postalCode1, double weight1, double costPerOunce1)
: senderName(senderName1),recipientName(recipientName1),address(address1),city(city1),state(state1),postalCode(postalCode1),weight(weight1),costPerOunce(costPerOunce1)
{
if(weight1>0)
{
weight=weight1;
}
else
{
weight=1;
cout << "Weight less than 0 !" << endl;
}
}
double Package::calculateCost() const
{
double transCosts;
transCosts=weight*costPerOunce;
return transCosts;
}
double Package::getWeight() const
{
return weight;
}
double Package::getCostPerOunce() const
{
return costPerOunce;
}
TwoDayPackage::TwoDayPackage(const string& senderName1,const string& recipientName1, const string& address1, const string& city1,const string& state1,int postalCode1, double weight1, double costPerOunce1,double surfaceMailCosts1)
:Package(senderName1,recipientName1,address1,city1,state1,postalCode1,weight1,costPerOunce1)
{
surfaceMailCosts=surfaceMailCosts1;
}
double TwoDayPackage::calculateCost() const
{
double transCosts;
transCosts = Package::calculateCost() + surfaceMailCosts;
return transCosts;
}
OvernightPackage::OvernightPackage(const string& senderName1,const string& recipientName1, const string& address1, const string& city1,const string& state1,int postalCode1, double weight1, double costPerOunce1,double extraCosts1)
:Package(senderName1,recipientName1,address1,city1,state1,postalCode1,weight1,costPerOunce1)
{
extraCosts=extraCosts1;
}
double OvernightPackage::calculateCost() const
{
double transCosts;
transCosts = Package::calculateCost() + extraCosts;
return transCosts;
}
//测试程序
int main()
{
Package text1("Lihua","Kandy","America","NewYork","NewYorkState",10041,30,5) ;
cout << "The cost of a regular package is :" << text1.calculateCost() << endl;
cout << endl;
TwoDayPackage text2("Zhang shan","Kandy","America","Houston","Texas",77001,50,8,25.6);
cout << "The cost of two-day package is : " << text2.calculateCost() << endl;
cout << endl;
OvernightPackage text3("","Kandy","America","St. Paul","Minnesota",35350,25,4,36.5);
cout << "The cost of overnight package is : " << text3.calculateCost() << endl;
cout << endl;
return 0;
}