C++ Package继承层次,采用继承实现快递包裹的分类计价(分为空运2日达、陆运3日达)。

一、问题描述:
  • Package继承层次,采用继承实现快递包裹的分类计价(分为空运2日达、陆运3日达)。自定义一个或多个快递公司,自定义计价方法,设计合适、合理的界面文本提示,以广东省内某市为起点,采用用户输入目的地点(省份或省份缩写等)、货物重量和快递时效(类型)的方式,计算快递运费,达到做成一个快递运费查询或者发快递的小软件。
  • 二、目的:
  • 1. 验证private、protect、public继承权限对数据成员和成员函数的权限影响;

    2. 掌握继承的优势,采用合适的继承方法,解决实际问题。

  • 三、问题具体解决方法:
  • 1、首先,创建基类Package,并在构造函数内对变量进行初始化,用a来判断用户所选择的快递为哪个。在各快递函数内定义各种费用。
  • class Package
    {
    public:
    	Package(double weight,int a,int b,int c)//Package的构造函数 
    	{
    		weight_m=weight;
    		this->c = c;
    		firstWeight=1;
    		switch(a)
    		{
    			case 1: EMS();	break;
    			case 2:	YTO();	break;
    			case 3: YUNDA();break;
    			case 4: ZTO();	break;
    			case 5: SF();	break;
    			case 6: STO();	break;
    		}
    	}
    	void EMS()//邮政 
    	{
    		if(c==1)//省内包裹 
    		{
    			LandFreight=6;
    		}
    		else//省外包裹 
    		{
    			LandFreight=8;	
    		} 
    		continuationWeight_Price=3;
    		AirFreight=10;
    	}
    	void YTO()//圆通 
    	{
    		if(c==1)//省内包裹 
    		{
    			LandFreight=8;
    		}
    		else//省外包裹 
    		{
    			LandFreight=10;	
    		} 
    		continuationWeight_Price=1.5;
    		AirFreight=12;
    	}
    	void YUNDA()//韵达 
    	{
    		if(c==1)//省内包裹 
    		{
    			LandFreight=8;
    		}
    		else//省外包裹 
    		{
    			LandFreight=10;	
    		} 
    		continuationWeight_Price=3;
    		AirFreight=9;
    	}		
    	void ZTO()//中通 
    	{
    		if(c==1)//省内包裹 
    		{
    			LandFreight=5;
    		}
    		else//省外包裹 
    		{
    			LandFreight=8;	
    		} 
    		continuationWeight_Price=2;
    		AirFreight=14;
    	}
    	void SF()//顺丰 
    	{
    		if(c==1)//省内包裹 
    		{
    			LandFreight=7;
    		}
    		else//省外包裹 
    		{
    			LandFreight=9;	
    		} 
    		continuationWeight_Price=2.5;
    		AirFreight=9;
    	}
    	void STO()//申通 
    	{
    		if(c==1)//省内包裹 
    		{
    			LandFreight=10;
    		}
    		else//省外包裹 
    		{
    			LandFreight=12;	
    		} 
    		continuationWeight_Price=1.5;
    		AirFreight=15;
    	}
    	double getLandFreight()
    	{
    		return LandFreight;
    	}
    	double getAirFreight()
    	{
    		return AirFreight;
    	}
    	double calculateFees(double firstWeight_Price)//计算快递费 
    	{
    		expressFee = firstWeight_Price+ (weight_m - firstWeight)*continuationWeight_Price;
    		return expressFee;
    	} 
    private:
    	double weight_m;//总重量
    	double firstWeight_Price;//首重价格 
    	double continuationWeight_Price;//续重价格 
    	double firstWeight;	//首重 
    	double continuationWeight;//续重 
    	double expressFee;//快递费 
    	double LandFreight;//陆运首重价格 
    	double AirFreight;//空运首重价格 
    	int c;
    };
    2、空运两日达类,是Package类的派生类,打印输出用户所需支付的快递费用。
  • class twoDayDeliver:protected Package//空运两日达 
    {
    public:	
    	twoDayDeliver(double weight_,int a_,int b_,int c_):Package(weight_,a_,b_,c_)
    	{	
    	}
    	void outputAmount()
    	{
    		cout<<"你所需支付的快递费为(空运): "<
    3、陆运三日达类,功能同上。
  • class threeDayDeliver:private Package//陆运三日达 
    {
    public:
    	threeDayDeliver(double weight_,int a_,int b_,int c_):Package(weight_,a_,b_,c_)
    	{	
    	}
    	void outputAmount()
    	{
    		
    		cout<<"你所需支付的快递费为(陆运): "<
    4、测试类的功能。注意:可以增加多一点交互。
  • int main()
    {
    	int k;
    	int a;//记录选择的快递公司的代号 
    	int b;//记录选择的寄件方式(空/陆) 
    	int c;//记录所寄件的省份的是否为省内 
    	string  destination;//记录目的地 
    	double weight;//记录包裹重量  
    	cout<<"********************下面为所提供的快递公司的具体收费情况:********************"<>k; 
    	while(k>0)
    	{
    		cout<<"请输入选择的快递公司的代号(1~6):"<>a;
    		cout<<"请输入所寄包裹的目的地(缩写开头字母):"<>destination;
    		cout<<"请输入所寄包裹的总重量(以斤为单位):"<>weight;
    		cout<<"请选择寄件方式(输入编号):1.陆运,2.空运 " <>b;
    		if(destination=="GD"||destination=="gd")//判断目的地是省内还是省外 
    		{
    			c=1;
    		}
    		else
    		{
    			c=0;
    		} 	
    		if(b==1)//判断选择的是陆运还是空运并计算运费 
    		{		
    			threeDayDeliver t2(weight,a,b,c);//创建了一个threeDayDeliver的对象(陆运) 
    			t2.outputAmount();	
    		}	
    		else
    		{
    			twoDayDeliver t3(weight,a,b,c);//创建了一个twoDayDeliver的对象(空运)
    			t3.outputAmount();
    		}
    		k--;
    		cout<
    四、完整代码。
  • #include
    #include
    using namespace std;
    class Package
    {
    public:
    	Package(double weight,int a,int b,int c)//Package的构造函数 
    	{
    		weight_m=weight;
    		this->c = c;
    		firstWeight=1;
    		switch(a)
    		{
    			case 1: EMS();	break;
    			case 2:	YTO();	break;
    			case 3: YUNDA();break;
    			case 4: ZTO();	break;
    			case 5: SF();	break;
    			case 6: STO();	break;
    		}
    	}
    	void EMS()//邮政 
    	{
    		if(c==1)//省内包裹 
    		{
    			LandFreight=6;
    		}
    		else//省外包裹 
    		{
    			LandFreight=8;	
    		} 
    		continuationWeight_Price=3;
    		AirFreight=10;
    	}
    	void YTO()//圆通 
    	{
    		if(c==1)//省内包裹 
    		{
    			LandFreight=8;
    		}
    		else//省外包裹 
    		{
    			LandFreight=10;	
    		} 
    		continuationWeight_Price=1.5;
    		AirFreight=12;
    	}
    	void YUNDA()//韵达 
    	{
    		if(c==1)//省内包裹 
    		{
    			LandFreight=8;
    		}
    		else//省外包裹 
    		{
    			LandFreight=10;	
    		} 
    		continuationWeight_Price=3;
    		AirFreight=9;
    	}		
    	void ZTO()//中通 
    	{
    		if(c==1)//省内包裹 
    		{
    			LandFreight=5;
    		}
    		else//省外包裹 
    		{
    			LandFreight=8;	
    		} 
    		continuationWeight_Price=2;
    		AirFreight=14;
    	}
    	void SF()//顺丰 
    	{
    		if(c==1)//省内包裹 
    		{
    			LandFreight=7;
    		}
    		else//省外包裹 
    		{
    			LandFreight=9;	
    		} 
    		continuationWeight_Price=2.5;
    		AirFreight=9;
    	}
    	void STO()//申通 
    	{
    		if(c==1)//省内包裹 
    		{
    			LandFreight=10;
    		}
    		else//省外包裹 
    		{
    			LandFreight=12;	
    		} 
    		continuationWeight_Price=1.5;
    		AirFreight=15;
    	}
    	double getLandFreight()
    	{
    		return LandFreight;
    	}
    	double getAirFreight()
    	{
    		return AirFreight;
    	}
    	double calculateFees(double firstWeight_Price)//计算快递费 
    	{
    		expressFee = firstWeight_Price+ (weight_m - firstWeight)*continuationWeight_Price;
    		return expressFee;
    	} 
    private:
    	double weight_m;//总重量
    	double firstWeight_Price;//首重价格 
    	double continuationWeight_Price;//续重价格 
    	double firstWeight;	//首重 
    	double continuationWeight;//续重 
    	double expressFee;//快递费 
    	double LandFreight;//陆运首重价格 
    	double AirFreight;//空运首重价格 
    	int c;
    };
    
    class twoDayDeliver:protected Package//空运两日达 
    {
    public:	
    	twoDayDeliver(double weight_,int a_,int b_,int c_):Package(weight_,a_,b_,c_)
    	{	
    	}
    	void outputAmount()
    	{
    		cout<<"你所需支付的快递费为(空运): "<>k; 
    	while(k>0)
    	{
    		cout<<"请输入选择的快递公司的代号(1~6):"<>a;
    		cout<<"请输入所寄包裹的目的地(缩写开头字母):"<>destination;
    		cout<<"请输入所寄包裹的总重量(以斤为单位):"<>weight;
    		cout<<"请选择寄件方式(输入编号):1.陆运,2.空运 " <>b;
    		if(destination=="GD"||destination=="gd")//判断目的地是省内还是省外 
    		{
    			c=1;
    		}
    		else
    		{
    			c=0;
    		} 	
    		if(b==1)//判断选择的是陆运还是空运并计算运费 
    		{		
    			threeDayDeliver t2(weight,a,b,c);//创建了一个threeDayDeliver的对象(陆运) 
    			t2.outputAmount();	
    		}	
    		else
    		{
    			twoDayDeliver t3(weight,a,b,c);//创建了一个twoDayDeliver的对象(空运)
    			t3.outputAmount();
    		}
    		k--;
    		cout<
    五、运行结果展示。
  • C++ Package继承层次,采用继承实现快递包裹的分类计价(分为空运2日达、陆运3日达)。_第1张图片

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