一、问题描述:
- 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<<"你所需支付的快递费为(空运): "<<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; cin>>k; while(k>0) { cout<<"请输入选择的快递公司的代号(1~6):"<<endl; cin>>a; cout<<"请输入所寄包裹的目的地(缩写开头字母):"<<endl; cin>>destination; cout<<"请输入所寄包裹的总重量(以斤为单位):"<<endl; cin>>weight; cout<<"请选择寄件方式(输入编号):1.陆运,2.空运 " <<endl; cin>>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<<endl; } return 0; }
四、完整代码。
-
#include<iostream> #include<cstring> 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<<"你所需支付的快递费为(空运): "<<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; cin>>k; while(k>0) { cout<<"请输入选择的快递公司的代号(1~6):"<<endl; cin>>a; cout<<"请输入所寄包裹的目的地(缩写开头字母):"<<endl; cin>>destination; cout<<"请输入所寄包裹的总重量(以斤为单位):"<<endl; cin>>weight; cout<<"请选择寄件方式(输入编号):1.陆运,2.空运 " <<endl; cin>>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<<endl; } return 0; }
五、运行情况展示。