当前位置: 首页 > news >正文

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<<"你所需支付的快递费为(空运): "<<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;
    }

    五、运行情况展示。

http://www.lryc.cn/news/216842.html

相关文章:

  • 中文大语言模型汇总
  • GEE:GEE中实现简单计算器
  • 概念解析 | 神经网络中的位置编码(Positional Encoding)
  • 【ubuntu】搭建lamp架构
  • GNU ld(链接器)的主要功能
  • springboot整合FTP实现文件传输
  • Spring Boot 2.x.x 升级至 Spring Boot 3.x.x
  • 光电直读水表支持短时间多次抄表吗
  • 家庭私人影院 - Windows搭建Emby媒体库服务器并远程访问 「无公网IP」
  • 核心舱在轨飞行VR沉浸式互动体验满足大家宇宙探险的心愿
  • k8s集群中namespace状态一直显示Terminating
  • 数据库高速缓存配置
  • 性能优化之懒加载 - 基于观察者模式和单例模式的实现
  • 【LeetCode刷题-链表】--1290.二进制链表转整数
  • 搭建Radius认证服务器 安当加密
  • Windows11恢复组策略编辑器功能的方法
  • Django实战项目-学习任务系统-查询列表分页显示
  • Git 拉取指定TAG/分支的代码
  • 2-爬虫-代理池搭建、代理池使用(搭建django后端测试)、爬取某视频网站、爬取某视频网站、bs4介绍和遍历文档树
  • 动手学深度学习——残差网络ResNet(原理解释+代码详解)
  • MYSQL 8.0 配置CDC(binlog)
  • 软件测试/测试开发丨ChatGPT能否成为PPT最佳伴侣
  • java对象的创建过程
  • Salesforce创建一个页面,能够配置各种提示语,而不需要修改代码
  • 轻松管理MySQL权限:Python脚本带你飞
  • Py之transformers_stream_generator:transformers_stream_generator的简介、安装、使用方法之详细攻略
  • 2023年Zotero最新同步教程-使用TeraCloud的25G免费空间实时跨设备同步文献
  • 面试题:用宏定义写出swap(x,y),即交换两数。
  • 微服务框架SpringcloudAlibaba+Nacos集成RabbitMQ
  • 低代码开发,一场深度的IT效率革命