C实验四多态程序设计_第1页
C实验四多态程序设计_第2页
C实验四多态程序设计_第3页
已阅读5页,还剩9页未读 继续免费阅读

下载本文档

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

文档简介

1、天津理工大学实验报告学院(系)名称: 计算机与通信工程学院姓 名学号专业计算机科学与技术班级教学二班实验项目实验四多态程序设计课程名称咼级程序设计语言II课程代码0667026实验时间2016年11月11日第7、8节实验地点计算机软件实验室 7-215批改意见成绩教师签字:实验目的:(1)理解类和对象的概念;(2)掌握类与对象的定义方法;(3)理解类的成员的访问控制的含义,公有和私有成员的区别;(4)掌握构造函数和析构函数的含义与作用、定义方式和实现;(5)能够根据给定的要求定义类并实现类的成员函数;(6)掌握string类的使用方法(7)了解C+面向对象程序设计的基本思想、基本方法和基本步骤

2、;(8)掌握 MS Visual C+6.0或DEV C+调试C+程序的基本方法、基本步骤。实验内容:1.疋义Point类,有坐标x,y两个成员变量,利用友兀函数对Point类重载 +运算符,头现对坐标值的改变。具体要求如下:(1)编与程序疋义Point类,在类中疋义整型的私有成员变量x,y ;(2)在类中定义两个友元函数,分别重载前置+和后置+ ;(3)编写主函数测试。注意函数有无返回值的区别,以及返回值是否带有&应用符号。代码:#include viostream>using namespace std;class Pointprivate:double x,y;public

3、:Point(double _x=0,double _y=0) x=_x;y=_y;Point()void setx(double _x)x=_x;void sety(double _y)y=_y; double getx()return x;double gety()return y;friend void operatorvv(ostream &out,const Point &p) outvv"Point("vvp.xvv","vvp.yvv")n"friend void operator+(Point &

4、; p,int); friend void operator+(Point & p);;void operator+(Point &p,int)p.x+;p.y+;void operator+(Point &p)+p.x;+p.y;int main() Point a(1,1),b(3,4); coutvva; coutvvb; a+;+b; coutvva; coutvvb; return 0;运行结果如下:C FA翹密W鞍匚+爆验源代码直題卧|Point(1J) Point(3,4) Point(2,2) Point(4,5)Process exited with

5、return value 0 Press any key to cont inue * . +2. 定义Point类,有坐标x,y两个成员变量,利用运算符重载对Point类重载“ + ”运算符,实现对坐标值的改变。具体要求如下:(1) 编写程序定义Point类,在类中定义整型的私有成员变量x,y ;(2) 定义成员函数 Point& operator+(); Point operator+(int);以实现对 Point 类重载“ + ”运算 符,分别重载前置+和后置+ ;(3) 编写主函数测试。源代码如下:#include viostream>using namespace s

6、td;class Pointprivate:double x,y;public:Point(double _x=0,double _y=0) x=_x;y=_y;Point()void setx(double _x)x= x;void sety(double _y)y=_y;double getx()return x;double gety() return y;friend void operatorvv(ostream &out,const Point &p) outvv"Point("vvp.xvv","vvp.yvv")

7、n"Point operator+(int)x+=2;y+=2;return *this;Point &operator+()+x;+y; return *this;int main()Point a(1,1),b(3,4);coutvva;coutvvb;a+;+b;coutvva;coutvvb;return 0;运行结果如下:3. 定义一个分数类,通过重载运算符实现分数的四则运算、求负运算和赋值运算。其中,要求加法+和减法用友元函数实现重载,其他运算符用成员函数实现重载。代码:#include viostream>#include <cmath>Usi

8、ng namespace std;class Fenshuprivate:int zi;int mu;public:Fenshu(int _zi=0,int _mu=1)zi=_zi;mu=_mu;Fenshu()void setzi(int _zi=0)zi=_zi;void setmu(int _mu=1)mu=_mu;int getzi()return zi;int getmu()return mu;void print();friend Fenshu operator+(const Fenshu & f1,const Fenshu & f2); friend Fensh

9、u operator-(const Fenshu & f1,const Fenshu & f2); Fenshu operator*(int n )Fenshu x;x.zi=zi*n;x.mu=mu; return x; Fenshu operator/(int n)if(n=0)coutvv"Nagetive!" return *this;Fenshu x;x.zi=zi;x.mu=mu*n; return x;Fenshu operator-。Fenshu x;x.zi=-zi;x.mu=mu; return x;void operator=(cons

10、t Fenshu & f)zi=f.zi; mu=f.mu;Fenshu operator+(const Fenshu & f1,const Fenshu & f2)Fenshu f;f.zi=f1.zi*f2.mu+f2.zi*f1.mu;f.mu=f1.mu*f2.mu;return f;Fenshu operator-(const Fenshu & f1,const Fenshu & f2)Fenshu f;f.zi=f1.zi*f2.mu-f2.zi*f1.mu;f.mu=f1.mu*f2.mu; return f;void Fenshu:pri

11、nt()if(zi=0)coutvv0vvendl;elseint _zi=abs(zi>mu?zi:mu),_mu=abs(mu<zi?mu:zi); int x;x=_zi % _mu;while(x!=0)_mu=_zi;_zi=x; x=_mu % _zi;zi/=(_zi>_mu?_mu:_zi); mu/=(_zi>_mu?_mu:_zi); coutvvzivv"/"vvmu<vendl;int main()Fenshu x(2,4),y(4,4);Fenshu z,a,b,c,d,e;z=x+y;a=y-x; b=x-y;c=-

12、x;d=y*(-1); e=y/2;乙 print();a. print();b. print();c. print();d. print();e. print(); return 0;运行结果如下:13/2 卩/21/2-1/2 -1/1>1/2Process exitd with re-turn value 0 rress any key to cc-nt inue 4. 编写程序,定义抽象基类Container,由此派生出2个派生类球体类 Sphere,圆柱体类Cylinder,分别用虚函数分别计算表面积和体积。.” ,243(1) 球体的表面积为:4二r,球体的体积为 一 r ;

13、圆柱表面积为:2 nR ( h+R)3圆柱体的体积 n R2h(2) 定义相应的对象,编写主函数测试。代码如下:#include viostream>using namespace std;const double Pl=3.14;class Containerprivate:double r;double h;public:Container()Container(double _r=0.0,double _h=0.0)r=_r;h=_h;void setr(double _r=0.0)r=_r;double getr()return r;void seth(double _h=0.0

14、)h=_h;double geth()return h;virtual double s()=0;virtual double v()=0;;class Sphere:public Containerpublic:Sphere()Sphere(double _r=0.0):Container(_r)double s()return 4*PI*getr()*getr();double v()return (double(4)/3)*PI*getr()*getr()*getr();class Cylinder:public Containerpublic:Cylinder()Cylinder(do

15、uble _r=0.0,double _h=0.0):Container(_r,_h) double s()return 2*PI*getr()*(geth()+getr();double v()return 2*PI*getr()*geth();;int main()Container *p;Sphere a(1);Cylinder b(1,2);P=& a;cout«"Sphere s="vvp->s()vv" v="vvp->v()vvendl; p=&b;coutvv"Cylinder s=&quo

16、t;vvp->s()vv" v="vvp->v()vvendl;return 0;运行结果如下:*F:笔商创鞍匚+僅验源代码实验四4心eSphere s=12.56 v=4.18667Cyl inder s=18.84 v=12.56Process exited with return value 0 Press any key to cont inje + *5. 设计一个时钟类 TIME,内含数据成员 hour, mi nu te , seco nd表示时间,成员函数set()设置时间数据,show()显示时间数据。重载运算符+和-(具有返回值),每执行一次

17、+,seco nd自增1,执行一次-,seco nd自减1。seco nd和mi nute的值在059区间循环(满59后再自增则归 0, min ute加1 ; seco nd为0时再自减则为 59, mi nute减1)。 hour的值在023区间循环。源代码如下:#include viostream>using namespace std;class Timeprivate:int hour;int minute;int second;public:Time(int=0,int=0,int=0);Time()void sethour(int);void setminute(int);

18、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(s>59)s%=60; m=m+s/60; if(m>59) m%=60; h=h+m /60;if(h>23) h%=24; hour=h; minute=m; second=s;void T

19、ime:sethour(int h)if(h>23) h%=24; hour=h;void Time:setminute(int m) if(minute>59)minute%=60; hour=hour+minute/60; if(hour>23) hour%=24; minute=m;void Time:setsecond(int s) if(s>59)minute=minute+s/60; s%=60; if(minute>59) hour=hour+minute/60; minute%=60; if(hour>23) hour%=24;second=

20、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(s>59)m=m+s/60; s%=60; if(m>59) h=h+m /60; m%=60;if(h>23) h%=24; hour=h;minute=m; second=s;void Time:show()coutvvhourvv":"vvminutevv":&quo

21、t;vvsecondvvendl;Time & Time:operator+(int)second+;if(second>59)minute=minute+second/60; second%=60;if(minute>59)hour=hour+minute /60; minute%=60;if(hour>23) hour%=24; return *this;Time & Time:operator-(int)second-;if(secondvO)minute-; second=59; if(minutevO)minute=59; hour-;if(hour

22、<0)hour=0; return *this;int main()Time t;int x,y,z;char temp;coutvv"现在初始化计数器(Hour Minute Second ): cin>>x;cin>>y;cin>>z;t.set(x,y,z);coutw"现在时间是:";t.show();docoutvv"输入*重新设置计数器;"vvendl;coutvv"输入+计数器递加,输入-计数器递减;"vvendl;coutvv"输入字母o计数器清零;"vvendl;coutvv"输入q退出计数器。"vvendl;coutvv"请输入 >"cin»temp;switch(temp)case '*':coutvv"现在初始化计数器(Hour Minute Second): cin>>x;cin>>y;cin>>z;t.set(x,y,z);coutvv"现在时间是:"t.show(); break;case '+':t+;coutvv"现在时间是:&

温馨提示

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

评论

0/150

提交评论