版权说明:本文档由用户提供并上传,收益归属内容提供方,若内容存在侵权,请进行举报或认领
文档简介
1、实验四 多态性一、实验目的1、掌握运算符重载的基本方法。2、掌握友元运算符函数和成员运算符函数的使用方法及两者之间的不同。3、学习虚函数的定义与使用方法。4、了解静态多态性和动态多态性。5、学习使用虚函数和继承实现动态多态性。二、试验内容1、编写一个程序,要求:(1)生明一个类Complex(复数类),定义类Complex的两个对象c1和c2,对象c1通过构造函数直接指定复数的实部和虚部(类私有数据成员为double类型:real和imag)为2.5及3.7,对象c2通过构造函数直接指定复数的实部和虚部为4.2及6.5;(2)定义友元运算符重载函数,它以c1、c2对象为参数,调用该函数时能返回
2、两个复数对象相加操作;(3)定义成员函数print,调用该函数时,以格式“real+imag i”输出当前对象的实部和虚部,例如:对象的实部和虚部分别是4.2和6.5,则调用print函数输出格式为:4.2+6.5 i;(4)编写主程序,计算出复数对象c1和c2相加结果,并将其结果输出。#includeclass complexprivate:double real;double imag;public:complex(double r=0.0,double i=0.0);void print();friend complex operator+(complex &a,complex &b);
3、complex:complex(double r,double i)real=r;imag=i;complex operator+(complex &a,complex &b)complex temp;temp.imag=a.imag+b.imag;temp.real=a.real+b.real;return temp;void complex:print()coutreal+imagiendl;int main()complex c1(2.5,3.7),c2(4.2,6.5),c3;c3=c1+c2;c3.print();return 0;2、编写一个程序,其中设计一个时间类Time,用来保
4、存时、分、秒等私有数据成员,通过重载操作符“+”实现两个时间的相加。要求将小时范围限制在大于等于0,分钟范围限制在059分,秒钟范围限制在059秒。提示:时间类Time的参考框架如下:class Time public: Time(int h=0,int m=0,int s=0);/构造函数 Time operator+(Time &);/运算符重载函数,实现两个时间的相加 Time operator+();/运算符重载函数,实现两个时间的相加 void disptime();/显示时间函数 private: int hours,minutes,seconds;#include#include
5、class Time public: Time(int h=0,int m=0,int s=0);/构造函数 Time operator+(Time &);/运算符重载函数,实现两个时间的相加 Time operator+();/运算符重载函数,实现两个时间的相加 void disptime();/显示时间函数 private: int hours,minutes,seconds;Time:Time(int h,int m,int s)hours=h;minutes=m;seconds=s;Time Time:operator +(Time &t1) Time p; p.seconds=sec
6、onds+t1.seconds; int t=p.seconds; p.seconds=t%60; p.minutes=minutes+t1.minutes+t/60; int m=p.minutes; p.minutes=m%60; p.hours=hours+t1.hours+m/60; return p;void Time:disptime() couthours时minutes分seconds秒endl;int main()Time t1(12,20,33),t2(5,17,58),t3;t3=t1+t2;cout时间t1endl;t1.disptime();cout时间t2endl;
7、t2.disptime();cout相加后的时间:endl;t3.disptime();return 0;3、写一个程序,定义抽象类Container:class Container protected: double radius; public: Container(double r);/抽象类Container的构造函数 virtual double surface_area()=0;/纯虚函数surface_area virtual double volume()=0;/纯虚函数volume ;【要求】建立3个继承Container的派生类:Sphere(球体)、Cylinder(圆柱
8、体)、Cube(正方体),让每一个派生类都包含虚函数surface_area()和volume(),分别用来球体、圆柱体和正方体的表面积和体积。要求写出主程序,应用C+的多态性,分别计算边长为6.0的正方体、半径为5.0的球体,以及半径为5.0和高为6.0的圆柱体的表面积和体积。#includeusing namespace std; class Container protected: double radius; public: Container(double r);/抽象类 Container 的构造函数 virtual double surface_area()=0;/纯虚函数sur
9、face_area virtual double volume()=0;/纯虚函数volume ; Container:Container(double r=0) radius=r; class Sphere:public Container public: Sphere(double x):Container(x) double surface_area() return 4*radius*radius*3.14; double volume() return 0.75*3.14*radius*radius*radius; ; class Cylinder:public Container
10、public: Cylinder(double h,double r):Container(r) hight=h; double surface_area() return 2*3.14*radius*radius+2*3.14*radius*hight; double volume() return 3.14*radius*radius*hight; protected: double hight; ; class Cube:public Container public: Cube(double r):Container(r) double surface_area() return 6*
11、radius*radius; double volume() return radius*radius*radius; ; int main() Container *p; Cube a(6.0); p=&a; cout正方体表面积:surface_area()endl; cout正方体体积:volume()endl; Sphere b(5.0); p=&b; cout球表面积:surface_area()endl; cout球体积:volume()endl; Cylinder c(6.0,5.0); p=&c; cout圆柱表面积:surface_area()endl; cout圆柱体体积:
12、volume()endl; return 0; 4、设计一个点类Point,其结构如下:(1) Point类表示二维平面点的集合,数据成员由点的坐标值表示,类型为int;(2) 三个重载构造函数:a) 一个是无参数的构造函数;b) 一个是带坐标值参数的构造函数,实现对数据成员的初始化;c) 一个是copy构造函数,实现用一个对象初始化本对象;(3) 两个重载成员函数:a) void offert(int , int ); 实现点的偏移,参数是偏移量;b) void offert(Point &); 实现点的偏移,参数Point类对象是偏移量;(4) 6个运算符重载函数:a) bool oper
13、ator = = (Point &);判断两个点对象是否相等;b) void operator + =(Point &);将两个点对象相加;c) void operator +();将当前对象自增1(前缀);d) void operator +(int );将当前对象自增1(后缀);e) friend Point& operator + (Point &, Point &);将两个点对象相加;f) friend Point &operator (Point &, Point &);将两个点对象相减;(5) 两个成员函数提供实例对象对私有数据的访问:a) int GetX();b) int Ge
14、tY();(6)公有成员函数void Display();输出对象的数据成员;#includeclass Pointprivate:int x;int y;public:Point() x=0; y=0;Point(int a,int b) x=a; y=b;Point(const Point &p)x=p.x; y=p.y;void offert(int,int);void offert(Point &);bool operator=(Point &);void operator+=(Point &);void operator+();void operator+(int); friend
15、Point& operator+(Point &, Point &);friend Point& operator-(Point &, Point &); void display(); int GetX() return x; int Gety() return y;void Point:offert(int a,int b) x+=a; y+=b;void Point:offert(Point &p) x+=p.x; y+=p.y;bool Point:operator=(Point &p) if(x=p.x&y=p.y) cout=endl; else cout!=endl; retur
16、n 0;void Point:operator+=(Point &p)x+=p.x;y+=y+p.y;void Point:operator+() x+;void Point:operator+(int) y+;Point&operator+(Point &a,Point &b) a.x=a.x+b.x;a.y=a.y+b.y; return a;Point&operator-(Point &a,Point &b) a.x=a.x-b.x;a.y=a.y-b.y; return a;void Point:display()cout(x,y)endl;int main() Point a(2,2),b(4,5),c(3,1),d,e,f,g(5,3),h;couta坐标;a.display(); coutb坐标;b.di
温馨提示
- 1. 本站所有资源如无特殊说明,都需要本地电脑安装OFFICE2007和PDF阅读器。图纸软件为CAD,CAXA,PROE,UG,SolidWorks等.压缩文件请下载最新的WinRAR软件解压。
- 2. 本站的文档不包含任何第三方提供的附件图纸等,如果需要附件,请联系上传者。文件的所有权益归上传用户所有。
- 3. 本站RAR压缩包中若带图纸,网页内容里面会有图纸预览,若没有图纸预览就没有图纸。
- 4. 未经权益所有人同意不得将文件中的内容挪作商业或盈利用途。
- 5. 人人文库网仅提供信息存储空间,仅对用户上传内容的表现方式做保护处理,对用户上传分享的文档内容本身不做任何修改或编辑,并不能对任何下载内容负责。
- 6. 下载文件中如有侵权或不适当内容,请与我们联系,我们立即纠正。
- 7. 本站不保证下载资源的准确性、安全性和完整性, 同时也不承担用户因使用这些下载资源对自己和他人造成任何形式的伤害或损失。
最新文档
- 2026年面酱创新技术突破与应用报告
- 2026年电子商务行业创新分析报告:重构商业生态
- 2026中国工业酶制剂超滤浓缩膜系统清洗规程标准化报告
- 2026年科环集团人员招聘笔试模拟试题及答案详解
- 2026册亨县农业农村局农技推广特聘人员遴选2人笔试模拟试题及答案详解
- 普通镗工风险评估与管理强化考核试卷含答案
- 工程机械装配调试工创新思维知识考核试卷含答案
- 国家能源投资集团有限责任公司2027年度高校毕业生直招(792人)笔试备考试题及答案详解
- 2026年合肥燃气集团有限公司人员招聘笔试模拟试题及答案详解
- 香料制造工冲突管理模拟考核试卷含答案
- 压力容器爆炸安全教育培训
- 2026年第四届全国人工智能应用技术技能大赛(工业视觉系统运维员赛项)理论考试题库(附答案)
- GB/T 48047-2026熔模铸件(铸钢、镍合金和钴合金)通用技术要求
- 2026年高考北京卷化学高考真题(含答案解析)
- 中国皮肤鳞状细胞癌诊疗指南(2026版)
- 数据安全分级分类制度
- 托管班转让合同协议书范本
- 新版2026年高考地理(陕晋青宁卷)真题详细解读及评析
- 团体标准邻甲氧基苯甲醛征求意见稿
- 旋挖桩施工质量管理方案
- 车辆动态监控奖惩制度
评论
0/150
提交评论