网站的关键词库,wordpress禁止谷歌字体大小,直接打开百度,成都最新通告今天一、问题描述#xff1a;
Package继承层次#xff0c;采用继承实现快递包裹的分类计价#xff08;分为空运2日达、陆运3日达#xff09;。自定义一个或多个快递公司#xff0c;自定义计价方法#xff0c;设计合适、合理的界面文本提示#xff0c;以广东省内某市为起点
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_mweight;this-c c;firstWeight1;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(c1)//省内包裹 {LandFreight6;}else//省外包裹 {LandFreight8; } continuationWeight_Price3;AirFreight10;}void YTO()//圆通 {if(c1)//省内包裹 {LandFreight8;}else//省外包裹 {LandFreight10; } continuationWeight_Price1.5;AirFreight12;}void YUNDA()//韵达 {if(c1)//省内包裹 {LandFreight8;}else//省外包裹 {LandFreight10; } continuationWeight_Price3;AirFreight9;} void ZTO()//中通 {if(c1)//省内包裹 {LandFreight5;}else//省外包裹 {LandFreight8; } continuationWeight_Price2;AirFreight14;}void SF()//顺丰 {if(c1)//省内包裹 {LandFreight7;}else//省外包裹 {LandFreight9; } continuationWeight_Price2.5;AirFreight9;}void STO()//申通 {if(c1)//省内包裹 {LandFreight10;}else//省外包裹 {LandFreight12; } continuationWeight_Price1.5;AirFreight15;}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你所需支付的快递费为空运: calculateFees(getAirFreight())endl;}
}; 3、陆运三日达类功能同上。 class threeDayDeliver:private Package//陆运三日达
{
public:threeDayDeliver(double weight_,int a_,int b_,int c_):Package(weight_,a_,b_,c_){ }void outputAmount(){cout你所需支付的快递费为陆运: calculateFees(getLandFreight())endl;}
}; 4、测试类的功能。注意可以增加多一点交互。 int main()
{int k;int a;//记录选择的快递公司的代号 int b;//记录选择的寄件方式(空/陆) int c;//记录所寄件的省份的是否为省内 string destination;//记录目的地 double weight;//记录包裹重量 cout********************下面为所提供的快递公司的具体收费情况********************endl*公司名称编号 续重价格: 陆运首重价格省内/省外 空运首重价格 *endl *邮政1 3/斤 6/8 10 *endl*圆通2 1.5/斤 8/10 12 *endl*韵达3 3/斤 8/10 9 *endl*中通4 2/斤 5/8 14 *endl*顺丰5 2.5/斤 7/9 9 *endl*申通6 1.5/斤 10/12 15 *endl******************************************************************************endl;cout请输入你想寄的包裹的个数endl;cink; while(k0){cout请输入选择的快递公司的代号1~6endl;cina;cout请输入所寄包裹的目的地(缩写开头字母)endl;cindestination;cout请输入所寄包裹的总重量(以斤为单位)endl;cinweight;cout请选择寄件方式输入编号1.陆运2.空运 endl;cinb;if(destinationGD||destinationgd)//判断目的地是省内还是省外 {c1;}else{c0;} if(b1)//判断选择的是陆运还是空运并计算运费 { threeDayDeliver t2(weight,a,b,c);//创建了一个threeDayDeliver的对象陆运 t2.outputAmount(); } else{twoDayDeliver t3(weight,a,b,c);//创建了一个twoDayDeliver的对象空运t3.outputAmount();}k--;coutendl;} return 0;
} 四、完整代码。 #includeiostream
#includecstring
using namespace std;
class Package
{
public:Package(double weight,int a,int b,int c)//Package的构造函数 {weight_mweight;this-c c;firstWeight1;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(c1)//省内包裹 {LandFreight6;}else//省外包裹 {LandFreight8; } continuationWeight_Price3;AirFreight10;}void YTO()//圆通 {if(c1)//省内包裹 {LandFreight8;}else//省外包裹 {LandFreight10; } continuationWeight_Price1.5;AirFreight12;}void YUNDA()//韵达 {if(c1)//省内包裹 {LandFreight8;}else//省外包裹 {LandFreight10; } continuationWeight_Price3;AirFreight9;} void ZTO()//中通 {if(c1)//省内包裹 {LandFreight5;}else//省外包裹 {LandFreight8; } continuationWeight_Price2;AirFreight14;}void SF()//顺丰 {if(c1)//省内包裹 {LandFreight7;}else//省外包裹 {LandFreight9; } continuationWeight_Price2.5;AirFreight9;}void STO()//申通 {if(c1)//省内包裹 {LandFreight10;}else//省外包裹 {LandFreight12; } continuationWeight_Price1.5;AirFreight15;}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你所需支付的快递费为空运: calculateFees(getAirFreight())endl;}
};class threeDayDeliver:private Package//陆运三日达
{
public:threeDayDeliver(double weight_,int a_,int b_,int c_):Package(weight_,a_,b_,c_){ }void outputAmount(){cout你所需支付的快递费为陆运: calculateFees(getLandFreight())endl;}
};int main()
{int k;int a;//记录选择的快递公司的代号 int b;//记录选择的寄件方式(空/陆) int c;//记录所寄件的省份的是否为省内 string destination;//记录目的地 double weight;//记录包裹重量 cout********************下面为所提供的快递公司的具体收费情况********************endl*公司名称编号 续重价格: 陆运首重价格省内/省外 空运首重价格 *endl *邮政1 3/斤 6/8 10 *endl*圆通2 1.5/斤 8/10 12 *endl*韵达3 3/斤 8/10 9 *endl*中通4 2/斤 5/8 14 *endl*顺丰5 2.5/斤 7/9 9 *endl*申通6 1.5/斤 10/12 15 *endl******************************************************************************endl;cout请输入你想寄的包裹的个数endl;cink; while(k0){cout请输入选择的快递公司的代号1~6endl;cina;cout请输入所寄包裹的目的地(缩写开头字母)endl;cindestination;cout请输入所寄包裹的总重量(以斤为单位)endl;cinweight;cout请选择寄件方式输入编号1.陆运2.空运 endl;cinb;if(destinationGD||destinationgd)//判断目的地是省内还是省外 {c1;}else{c0;} if(b1)//判断选择的是陆运还是空运并计算运费 { threeDayDeliver t2(weight,a,b,c);//创建了一个threeDayDeliver的对象陆运 t2.outputAmount(); } else{twoDayDeliver t3(weight,a,b,c);//创建了一个twoDayDeliver的对象空运t3.outputAmount();}k--;coutendl;} return 0;
} 五、运行情况展示。