实验报告模板-实验4多态程序设计_第1页
实验报告模板-实验4多态程序设计_第2页
实验报告模板-实验4多态程序设计_第3页
实验报告模板-实验4多态程序设计_第4页
实验报告模板-实验4多态程序设计_第5页
已阅读5页,还剩14页未读 继续免费阅读

下载本文档

版权说明:本文档由用户提供并上传,收益归属内容提供方,若内容存在侵权,请进行举报或认领

文档简介

1、天津理工大学计算机与通信工程学院实验报告 至 学年 第 学期课程名称实验( 4 )实验名称实验时间学号姓名专业主讲教师辅导教师软件环境硬件环境实验目的(1)掌握运算符重载的方法;(2)掌握使用虚函数实现动态多态性;实验内容(应包括实验题目、实验要求、实验任务等)1.定义Point类,有坐标x,y两个成员变量,利用友元函数对Point类重载“+”运算符,实现对坐标值的改变。具体要求如下:(1)编写程序定义Point类,在类中定义整型的私有成员变量x,y;(2)在类中定义两个友元函数,分别重载前置+和后置+;(3)编写主函数测试。注意函数有无返回值的区别,以及返回值是否带有&应用符号。2.定义Po

2、int类,有坐标x,y两个成员变量,利用运算符重载对Point类重载“+”运算符,实现对坐标值的改变。具体要求如下:(1)编写程序定义Point类,在类中定义整型的私有成员变量x,y;(2)定义成员函数Point& operator+(); Point operator+(int);以实现对Point类重载“+”运算符,分别重载前置+和后置+;(3)编写主函数测试。3.定义一个分数类,通过重载运算符实现分数的四则运算、求负运算和赋值运算。其中,要求加法“+” 和减法“-”用友元函数实现重载,其他运算符用成员函数实现重载。4.定义一个车(vehicle)基类,有Run、Stop等成员函数,由此派

3、生出自行车(bicycle)类、汽车(motorcar)类,从bicycle和motorcar派生出摩托车(motorcycle)类,它们都有Run、Stop等成员函数。具体要求如下:(1)编写程序定义一个车(vehicle)基类,有Run、Stop等成员函数;(2)由车(vehicle)基类派生出自行车(bicycle)类、汽车类(motorcar),从bicycle类和motorcar类派生出摩托车类(motorcycle),它们都有Run、Stop等成员函数。(3)在main()函数中定义vehicle、bicycle、motorcar、motorcycle的对象,调用其Run()、St

4、op()函数,观察其执行情况。(4)分别用vehicle类型的指针来调用几个对象的成员函数,看看能否成功(提示:把Run、Stop定义为虚函数)。5.编写程序,定义抽象基类Container,由此派生出2个派生类球体类Sphere,圆柱体类Cylinder,分别用虚函数分别计算表面积和体积。(1)球体的表面积为:4r2,球体的体积4r3 /3p; 圆柱表面积为: 2R(h+R) 圆柱体的体积R2h。(2)定义相应的对象,编写主函数测试。6.设计一个时钟类TIME,内含数据成员hour,minute,second表示时间,成员函数set( )设置时间数据,show( )显示时间数据。重载运算符

5、+和- (具有返回值),每执行一次+,second自增1,执行一次-,second自减1。second和minute的值在059区间循环(满59后再自增则归0,minute加1;second为0时再自减则为59,minute减1)。hour的值在023区间循环。实验过程与实验结果(可包括实验实施的步骤、算法描述、流程、结论等)1.记录程序运行结果2.记录程序运行结果3.记录程序运行结果4.记录程序运行结果5.记录程序运行结果6. 记录程序运行结果19 / 19文档可自由编辑打印附录(可包括源程序清单或其它说明)1.#includeusing namespace std; class point

6、 private: int x, y;public: point(int, int);friend void operator+(point&);friend void operator+(point&, int); void display(); ; point:point(int a = 0, int b = 0) :x(a), y(b) void point:display() cout x= x t y= yendl; void operator+(point& A)+A.x; +A.y; void operator+(point& A, int) +A.x; +A.y; int ma

7、in()point A(1, 2); +A; cout +A之后:; A.display();A+; cout A+之后:; A.display();return 0;2.#includeusing namespace std; class point private: int x, y;public: point(int, int);point& operator+(); point operator+(int); void display(); ;point:point(int a = 0, int b = 0) :x(a), y(b) void point:display() cout

8、x= x t y= yendl; point& point:operator+() +x; +y; return *this; point point:operator+(int) +x; +y; return *this; int main() point A(1, 2);+A;cout +A之后:; A.display(); A+; cout A+之后:; A.display();return 0;3.#includeusing namespace std;class fenshu int fz, fm; public: fenshu(int i = 0, int j = 0); void

9、 huajian(); void setData()cout 输入分子: fz; cout 输入分母: fm; friend fenshu operator+(fenshu &a, fenshu &b);friend fenshu operator-(fenshu &a, fenshu &b); fenshu operator*(fenshu &a); fenshu operator/(fenshu &a);fenshu &operator=(fenshu &); void show()huajian(); if (fz%fm = 0) cout (fz / fm) endl; else co

10、ut fz / fm endl; ;fenshu:fenshu(int i, int j) fz = i; fm = j; void fenshu:huajian()int t; while (fm = 0)cout 分母不能为零,请重新输入: endl;setData(); if (fz = fm) t = fz; else t = fm; for (int i = 2; i = t;) if (fz%i = 0 & fm%i = 0) fz = fz / i; fm = fm / i; else i+; fenshu operator+(fenshu &a, fenshu &b) fens

11、hu temp; temp.fz = a.fz*b.fm + b.fz*a.fm; temp.fm = a.fm*b.fm; return temp; fenshu operator-(fenshu &a, fenshu &b) fenshu temp; temp.fz = a.fz*b.fm - b.fz*a.fm; temp.fm = a.fm*b.fm; return temp; fenshu fenshu:operator *(fenshu &a) fenshu temp;temp.fz = a.fz*fz; temp.fm = a.fm*fm; return temp;fenshu

12、fenshu:operator /(fenshu &a)fenshu temp; temp.fz = a.fz*fm;temp.fm = a.fm*fz; return temp; fenshu &fenshu:operator=(fenshu &a) fz = a.fz; fm = a.fm; return *this; void main() fenshu A, B, A1, A2, A3, A4, A5; A.setData(); A.huajian(); A.show(); cout endl; B.setData(); B.huajian(); B.show(); cout endl

13、; A1 = A + B;A1.show(); A2 = A - B; A2.show();A3 = A*B; A3.show();A4 = A / B;A4.show(); A5 = A; A5.show();4.#includeusing namespace std;class vehicledouble speed;public: vehicle(int a):speed(a) virtual void Run(); virtual void Stop(); ; void vehicle:Run() cout speed= speed t;void vehicle:Stop() cout

14、 speed=1;class bicycle :virtual public vehicle private: double speed;public: virtual void Run(); virtual void Stop(); bicycle(int a) :vehicle(a) speed = a; ;void bicycle:Run() cout speed= speed t;void bicycle:Stop() cout speed=2;class motorcar :virtual public vehicleprivate: double speed;public: vir

15、tual void Run(); virtual void Stop(); motorcar(int a) :vehicle(a) speed = a;void motorcar:Run() cout speed= speed t; void motorcar:Stop() cout speed=3;class motorcycle :public bicycle, public motorcar private: double speed;public: void Run(); void Stop(); motorcycle(int a) :vehicle(a), bicycle(a), m

16、otorcar(a) speed = a; void motorcycle:Run() cout speed= speed t; void motorcycle:Stop() cout speed=4; int main() vehicle A(100), *p; bicycle AB(90); motorcar AC(80); motorcycle ABC(70); cout A:; A.Run(); A.Stop(); cout n AB:; AB.Run(); AB.Stop(); cout n AC:;AC.Run();AC.Stop(); cout n ABC:; ABC.Run()

17、; ABC.Stop(); cout n 用指针调用:; cout n Run();p-Stop(); cout n Run();p-Stop(); cout n Run(); p-Stop(); cout n Run();p-Stop(); cout n ;return 0;5.#include using namespace std; class Container virtual double area()=0; virtual double volume()=0;public:virtual void show()=0; class Sphere :public Containerpr

18、ivate: double r;public:Sphere(double a) :r(a) double area() return 4 * 3.14*r*r; double volume() return (double)4 / 3 * 3.14*r*r*r; void show()cout 球面积为: area() endl; cout 球体积为: volume() endl;class Cylinder :public Containerprivate: double r, h;public:Cylinder(double a = 0, double b = 0) :r(a), h(b)

19、 double area()return 2 * 3.14*r*(h + r); double volume() return 3.14*r*r*h; void show() cout 圆柱体面积为: area() endl; cout 圆柱体体积为: volume() show();Cylinder B(1, 2); pr = &B; pr-show(); return 0;6.#include using namespace std;class Timeprivate:int hour;int minute;int second;public:Time(int = 0, int = 0,

20、int = 0);Time() void sethour(int);void setminute(int);void setsecond(int);int gethour();int getsecond();int getminute();void set(int = 0, int = 0, int = 0);void show();Time& operator+(int);Time& operator-(int);Time:Time(int h, int m, int s)if (s59)s %= 60;m = m + s / 60;if (m59)m %= 60;h = h + m / 6

21、0;if (h23) h %= 24;hour = h;minute = m;second = s;void Time:sethour(int h)if (h23) h %= 24;hour = h;void Time:setminute(int m)if (minute59)minute %= 60;hour = hour + minute / 60;if (hour23) hour %= 24;minute = m;void Time:setsecond(int s)if (s59)minute = minute + s / 60;s %= 60;if (minute59)hour = h

22、our + minute / 60;minute %= 60;if (hour23) hour %= 24;second = s;int Time:gethour()return hour;int Time:getsecond()return second;int Time:getminute()return minute;void Time:set(int h, int m, int s)if (s59)m = m + s / 60;s %= 60;if (m59)h = h + m / 60;m %= 60;if (h23) h %= 24;hour = h;minute = m;second = s;void Time:show()cout hour : minute : second 59)minute

温馨提示

  • 1. 本站所有资源如无特殊说明,都需要本地电脑安装OFFICE2007和PDF阅读器。图纸软件为CAD,CAXA,PROE,UG,SolidWorks等.压缩文件请下载最新的WinRAR软件解压。
  • 2. 本站的文档不包含任何第三方提供的附件图纸等,如果需要附件,请联系上传者。文件的所有权益归上传用户所有。
  • 3. 本站RAR压缩包中若带图纸,网页内容里面会有图纸预览,若没有图纸预览就没有图纸。
  • 4. 未经权益所有人同意不得将文件中的内容挪作商业或盈利用途。
  • 5. 人人文库网仅提供信息存储空间,仅对用户上传内容的表现方式做保护处理,对用户上传分享的文档内容本身不做任何修改或编辑,并不能对任何下载内容负责。
  • 6. 下载文件中如有侵权或不适当内容,请与我们联系,我们立即纠正。
  • 7. 本站不保证下载资源的准确性、安全性和完整性, 同时也不承担用户因使用这些下载资源对自己和他人造成任何形式的伤害或损失。

评论

0/150

提交评论