C++ 多态:Package继承层次

【问题描述】


使用在上篇创建的Package类继承层次结构,创建一个用于显示若干Package的地址信息并计算其运输费用的程序。程序应该包含一个Package指针的vector 对象,其中的指针指向TwoDayPackage 对象和OvernightPackage 对象。遍历该vector 对象,多态性地处理这些Package。对于每个Package调用获取函数,获得发送者和接收者的地址信息,然后打印输出这两个地址,就像它们出现在邮包标签上一样。此外,调用每个Package的calculateCost成员函数并输出结果。跟踪记录该vector 中所有 Package的总的运输费用,并在循环遍历结束时显示此总费用。


1.创建基类Package

class Package
{
	public://声明为常量引用,使用引用避免copy,使用const能保护数据 
		Package(){}
		Package(const string&,	const string&, const string&, const string&,const string&, int , double , double);
		double getWeight() const;
		double getCostPerOunce() const;
		virtual double calculateCost()
		{
			double transCosts;
			transCosts=weight*costPerOunce;
			return transCosts;
		}
		virtual void get(){}
	protected:
		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(){}
		TwoDayPackage(const string&, const string&, const string&, const string&,const string&, int , double , double, double);
		virtual double calculateCost() //基类的成员函数需要在派生类内声明才可被使用 
		{
			double transCosts;
			
			transCosts = Package::calculateCost()  + surfaceMailCosts;
			
			return transCosts;
		}
		virtual void get()
		{
			cout<<"寄件人信息:"<
3.OvernightPackage类,继承于Package类
class OvernightPackage : public Package
{
	public:
		OvernightPackage(){}
		OvernightPackage(const string&, const string&, const string&, const string&,const string&, int , double , double, double);
		virtual double calculateCost() 
		{
			double transCosts;
			
			transCosts = Package::calculateCost()  + extraCosts;
			
			return transCosts;	
		}
		virtual void get()
		{
			cout<<"寄件人信息:"<
4.Package类的具体函数实现
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::getWeight() const
{
	return weight;
}
double Package::getCostPerOunce() const
{
	return costPerOunce;
}
5.TwoDayPackage类的方法实现
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; 										
}
6.OvernightPackage类的方法实现
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; 
}

7.测试程序

//测试程序 
int main()
{	
	TwoDayPackage *ptr_Two=new TwoDayPackage("Lihua","Kandy","America","NewYork","NewYorkState",10041,30,5,36.2);
	OvernightPackage *ptr_Over=new OvernightPackage("Zhang shan","Kandy","America","Houston","Texas",77001,50,8,25.6);
	vector  PackageClass;
	PackageClass.push_back(ptr_Two);
	PackageClass.push_back(ptr_Over);
	vector::iterator iter;
	for(iter = PackageClass.begin(); iter != PackageClass.end(); ++iter)
    {
    	cout<<"该快递的具体信息如下:"<get();
        cout<<"该包裹的运输费用为: "<<(*iter)->calculateCost()<

完整的代码如下:

///
//作业13.15(基于12.9) 
//Package继承层次 
//
#include
#include
#include
#include
using namespace std;
class Package
{
	public://声明为常量引用,使用引用避免copy,使用const能保护数据 
		Package(){}
		Package(const string&,	const string&, const string&, const string&,const string&, int , double , double);
		double getWeight() const;
		double getCostPerOunce() const;
		virtual double calculateCost()
		{
			double transCosts;
			transCosts=weight*costPerOunce;
			return transCosts;
		}
		virtual void get(){}
	protected:
		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(){}
		TwoDayPackage(const string&, const string&, const string&, const string&,const string&, int , double , double, double);
		virtual double calculateCost() //基类的成员函数需要在派生类内声明才可被使用 
		{
			double transCosts;
			
			transCosts = Package::calculateCost()  + surfaceMailCosts;
			
			return transCosts;
		}
		virtual void get()
		{
			cout<<"寄件人信息:"<0)
	{
		weight=weight1;	
	}
	else
	{
		weight=1;
		cout << "Weight less than 0 !" << endl;
	}	
} 
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; 										
}


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; 
}	
//测试程序 
int main()
{	
	TwoDayPackage *ptr_Two=new TwoDayPackage("Lihua","Kandy","America","NewYork","NewYorkState",10041,30,5,36.2);
	OvernightPackage *ptr_Over=new OvernightPackage("Zhang shan","Kandy","America","Houston","Texas",77001,50,8,25.6);
	vector  PackageClass;
	PackageClass.push_back(ptr_Two);
	PackageClass.push_back(ptr_Over);
	vector::iterator iter;
	for(iter = PackageClass.begin(); iter != PackageClass.end(); ++iter)
    {
    	cout<<"该快递的具体信息如下:"<get();
        cout<<"该包裹的运输费用为: "<<(*iter)->calculateCost()<

你可能感兴趣的:(C++,c++,算法,开发语言)