版权说明:本文档由用户提供并上传,收益归属内容提供方,若内容存在侵权,请进行举报或认领
文档简介
1、实验5 多态性和运算符重载一、 姓名 班级 电信13F 学号 二、 实验目的通过vc开发环境下派生类多态性的设计与实现,了解运行时多态的概念,掌握派生类多态性基本设计和编程方法。三、 实验主要内容1、 有三角形、正方形和圆形三种图形,求它们各自的面积。可先抽象出一个基类,在基类中声明一个虚函数,用于求面积,并利用单界面设计各个图形求面积的方法。2、 使用成员函数重载实现复数四则运算、逻辑运算和关系运算;3、 使用友元函数重载实现字符串连接运算,赋值运算;四、 实验程序1. #include<iostream>#include<math.h>using namespace
2、 std;#define PI 3.14class basepublic:virtual double area()return 0;class triangle:public baseprotected:double h; double w;public:triangle(double H,double W)h=H; w=W;double area()return 0.5*h*w;class square:public baseprotected:double a;public:square(double A)a=A;double area()return a*a;class circle:
3、public baseprotected:double radius;public:circle(double Rad)radius=Rad;double area()return PI * radius * radius;void main()base *a,*b,*c;a=new triangle(2,8);b=new square(9);c=new circle(5);cout<<a->area ()<<endl;cout<<b->area ()<<endl;cout<<c->area ()<<en
4、dl; 2. #include<iostream>using namespace std;class complex /复数protected:double real; /实部 double imag; /虚部public:complex();complex(double Real,double Imag)real=Real; imag=Imag;complex operator+ (complex&); /使用成员函数重载实现复数加法运算complex operator- (complex&); /使用成员函数重载实现复数减法运算complex operator*
5、 (complex&); /使用成员函数重载实现复数乘法运算complex operator/ (complex&); /使用成员函数重载实现复数除法运算 complex operator&&(complex&); /使用成员函数重载实现与运算complex operator|(complex&); /使用成员函数重载实现或运算 complex operator! (); /使用成员函数重载实现非运算 complex operator<(complex&); /使用成员函数重载实现或运算void print( complex&
6、; c) /输出结果if(c.imag<0) cout<<c.real<<c.imag<<'i'<<"n"else cout<<c.real<<'+'<<c.imag<<'i'<<"n" ;complex complex: operator+ (complex& c) /加法return complex(real+c.real,imag+c.imag);complex complex:
7、operator- (complex& c) /减法return complex(real-c.real,imag-c.imag);complex complex: operator* (complex& c) /乘法return complex(real * c.real - imag * c.imag, real * c.imag + imag * c.real); complex complex: operator/ (complex &c) /除法return complex(real * c.real + imag + c.imag) / (c.real *
8、c.real + c.imag * c.imag),(imag * c.real - real * c.imag) / (c.real * c.real + c.imag * c.imag); complex complex: operator&& (complex& c) /与运算return complex(real&&c.real,imag&&c.imag);complex complex: operator| (complex& c) /或运算return complex(real|c.real,imag|c.imag);
9、complex complex: operator! () /非运算return complex(!real,!imag);complex complex: operator< (complex& c) /小于运算if(real<c.real) return complex(real,imag);else return complex(c.real,c.imag);void main()complex c1(1.0, -0.0), c2(3.0, -1.0), c3; c3 = c1 + c2; cout<<"c1+c2=" c3.print
10、(c3);c3 = c1 - c2; cout<<"c1-c2=" c3.print(c3);c3 = c1 * c2; cout<<"c1*c2=" c3.print(c3);c3 = c1 / c2; cout<<"c1/c2=" c3.print(c3);c3 = c1&&c2; cout<<"c1&&c2=" c3.print(c3);c3 = c1|c2; cout<<"c1|c2=" c3.p
11、rint(c3);c3=!c1; cout<<"!c1=" c3.print(c3);c3=c1<c2; cout<<"输出实部较小的复数=" c3.print(c3); 3. #include<iostream>#include<string.h>using namespace std;class employee /员工类protected:char name20; /员工名字char id20; /员工号double salary; /员工薪水public:employee();employee
12、(char* emp_name,char* emp_id) /建立员工基本信息strcpy(name,emp_name); strcpy(id,emp_id);char* get_name() /获取名字return name;char* get_id() /获取职工号return id;double get_salary() /应得薪水return salary;class hourlyworker:public employee /时薪工类protected:int hours;double hourpay;public:hourlyworker();hourlyworker(char*
13、hour_name,char* hour_id,int hour,double pay):employee(hour_name,hour_id)hours=hour; hourpay=pay;char *get_name()return employee:get_name ();char *id()return employee:get_id ();int get_hours() /获取工作时间return hours;double get_hourpay() /获取小时工资return hourpay;double get_salary() /时薪工工资计算if(hours<=50)s
14、alary=hourpay*hours;elsesalary=hourpay*50+2*hourpay*(hours-50);return salary;class pieceworker:public employee /计件工类protected:int pieces; /工件数double piecepay; /每个工件赢得的工资public:pieceworker();pieceworker(char* piece_name,char* piece_id,int piece,double piece_pay):employee(piece_name,piece_id)pieces=pi
15、ece; piecepay=piece_pay;char *get_name()return employee:get_name ();char *id()return employee:get_id ();int get_pieces() /获取工件数return pieces; double get_pay() /每件的工资return piecepay;double piece_salary() /计件工工资salary=pieces*piecepay;return salary;class manager:public employeepublic:manager(char* man_
16、name,char* man_id):employee(man_name,man_id)char *get_name()return employee:get_name ();char *id()return employee:get_id ();double manager_salary() /经理工资return salary=3000; class commissionworker:public employee /佣金工类protected:double basesalary; /保底新书double total; /一周内的销售总额double percent; /提成的额度publ
17、ic:commissionworker();commissionworker(char* com_name,char* com_id,double com_basesalary,double com_total,double com_percent):employee(com_name,com_id)basesalary=com_basesalary; total=com_total; percent=com_percent;char *get_name()return employee:get_name ();char *id()return employee:get_id ();doubl
18、e get_base_pay() /保底薪水return basesalary;double get_com_total() /销售总额return total;double get_com_percent() /销售额中提成return percent;double com_salary() /佣金工的工资salary=basesalary+total*percent;return salary;void main() cout<<"输出时薪工信息:"<<"n" hourlyworker hworker("may&qu
19、ot;,"1413250",75,20.5); cout<<" hourlyworker'名字: "<<hworker.get_name ()<<"n" cout<<" hourlyworker'职工号: "<<hworker.get_id ()<<"n" cout<<" hourlyworker'工作小时数:"<<hworker.get_hours ()&
20、lt;<"n" cout<<" hourlyworker'每小时金额:"<<hworker.get_hourpay ()<<"n" cout<<" hourlyworker'总工资: "<<hworker.get_salary ()<<"n" cout<<"*"<<"n" cout<<"输出计件工信息:"&l
21、t;<"n" pieceworker pworker("march","1413258",100,25.5); cout<<" pieceworker'名字: "<<pworker.get_name ()<<"n" cout<<" pieceworker'职工号: "<<pworker.get_id ()<<"n" cout<<" piece
22、worker'工件数: "<<pworker.get_pieces ()<<"n" cout<<" pieceworker'每件的金额:"<<pworker.get_pay ()<<"n" cout<<" pieceworker'总工资: "<<pworker.piece_salary ()<<"n" cout<<"*"<<
23、"n" cout<<"输出经理信息:"<<"n" manager manag("Jack","000003"); cout<<" manager'名字: "<<manag.get_name ()<<"n" cout<<" manager'职工号:"<<manag.get_id ()<<"n" cout<
24、;<" manager'总工资:"<<manag.manager_salary ()<<"n" cout<<"*"<<"n" cout<<"输出佣金工信息:"<<"n" commissionworker cworker("Jim","1413257",578,254,0.01); cout<<" commissionworker&
25、#39;名字: "<<cworker.get_name ()<<"n" cout<<" commissionworker'职工号: "<<cworker.get_id ()<<"n" cout<<" commissionworker'保底工资:"<<cworker.get_base_pay ()<<"n" cout<<" commissionworker'销售额: "<<cworker.get_com_total ()<<"n" cout<<" commissionworker'提成额度:&qu
温馨提示
- 1. 本站所有资源如无特殊说明,都需要本地电脑安装OFFICE2007和PDF阅读器。图纸软件为CAD,CAXA,PROE,UG,SolidWorks等.压缩文件请下载最新的WinRAR软件解压。
- 2. 本站的文档不包含任何第三方提供的附件图纸等,如果需要附件,请联系上传者。文件的所有权益归上传用户所有。
- 3. 本站RAR压缩包中若带图纸,网页内容里面会有图纸预览,若没有图纸预览就没有图纸。
- 4. 未经权益所有人同意不得将文件中的内容挪作商业或盈利用途。
- 5. 人人文库网仅提供信息存储空间,仅对用户上传内容的表现方式做保护处理,对用户上传分享的文档内容本身不做任何修改或编辑,并不能对任何下载内容负责。
- 6. 下载文件中如有侵权或不适当内容,请与我们联系,我们立即纠正。
- 7. 本站不保证下载资源的准确性、安全性和完整性, 同时也不承担用户因使用这些下载资源对自己和他人造成任何形式的伤害或损失。
最新文档
- 金融安全与诚信教育
- 石河子大学《中药鉴定学实验一》2021-2022学年第一学期期末试卷
- 石河子大学《药物分析》2022-2023学年第一学期期末试卷
- 食品安全十三五规划
- 石河子大学《计算机组织与结构》2022-2023学年期末试卷
- 石河子大学《儿科学与儿童保健学见习》2021-2022学年第一学期期末试卷
- 沈阳理工大学《三维工程软件》2023-2024学年第一学期期末试卷
- 沈阳理工大学《建筑结构》2022-2023学年第一学期期末试卷
- 2018年四川内江中考满分作文《我心中的英雄》
- 沈阳理工大学《含能材料》2021-2022学年第一学期期末试卷
- 2024年安能物流合作加盟协议版
- 质量管理体系过程方法和风险思维专业解读与应用之7:5 领导作用-5.3组织的岗位、职责和权限(雷泽佳编制-2024B1)
- 地面找平专项施工方案
- 初三化学-水的净化省公开课获奖课件说课比赛一等奖课件
- 2024-2030年中国财税服务行业市场深度调研及发展前景与投资研究报告
- 第二次月考卷-2024-2025学年统编版语文六年级上册
- 申论国家公务员考试试题与参考答案
- 乱扔垃圾的课件
- 2024-2030年中国安全校车市场发展分析及市场趋势与投资方向研究报告
- 数字孪生水利项目建设可行性研究报告
- 北京市房山区2023-2024学年高二上学期期中地理试题 含解析
评论
0/150
提交评论