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

下载本文档

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

文档简介

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 using namespace std;class Pointprivat

3、e: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 operator(ostream &out,const Point &p) outPoint(p.x,p.y)n;friend void operator+(Point &p,int); friend void operator+(Point

4、&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); couta; coutb;a+;+b; couta; coutb; return 0; 运行结果如下:2. 定义 Point 类,有坐标 x,y 两个成员变量,利用运算符重载对Point 类重载“ + ”运算符,实现对坐标值的改变。具体要求如下:( 1) 编写程序定义 Point 类,在类中定义整型的私有成员变量x,y;( 2) 定义成员函数 Point& operator+();

5、 Point operator+(int); 以实现对 Point 类重载“ +”运算 符,分别重载前置 +和后置 + ;( 3) 编写主函数测试。源代码如下:#include using namespace std;class Point private: 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 voi

6、d operator(ostream &out,const Point &p) outPoint(p.x,p.y)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); couta; coutb;a+;+b; couta; coutb;return 0; 运行结果如下:3. 定义一个分数类, 通过重载运算符实现分数的四则运算、 求负运算和赋值运算。 其中, 要求加法 “+ 和减法 “-”用友元函数实现重载,其他运算符用成员

7、函数实现重载。代码:#include #include Using 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 Fens

8、hu& f2); friend Fenshu 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)coutNagetive!;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=(const F

9、enshu & 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:print() if(zi=0)cout0mu?zi:

10、mu),_mu=abs(mu_mu?_mu:_zi); mu/=(_zi_mu?_mu:_zi); coutzi/muendl; 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=-x; d=y*(-1); e=y/2; z.print();a. print();b. print();c. print();d. print();e. print(); return 0; 运行结果如下:4. 编写程序,定义抽象基类 Container ,由此派生出 2 个派生类球体类 Sphere,圆柱体类 Cy

11、linder ,分 别用虚函数分别计算表面积和体积。2 4 3( 1) 球体的表面积为: 4 r 2 ,球体的体积为 r 3 ; 圆柱表面积为: 2R( h+R) 3 圆柱体的体积 R2h。( 2) 定义相应的对象,编写主函数测试。 代码如下:#include using namespace std; const double PI=3.14;class Container private: double r; double h;public: Container()Container(double _r=0.0,double _h=0.0)r=_r; h=_h;void setr(doubl

12、e _r=0.0)r=_r;double getr()return r;void seth(double _h=0.0)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

13、();class Cylinder:public Container public:Cylinder()Cylinder(double _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; coutSphere s=s() v=v()endl; p=&b;coutCylinder s=s() v=v

14、()endl;return 0; 运行结果如下:5. 设计一个时钟类 TIME ,内含数据成员 hour,minute,second表示时间,成员函数 set( )设置时间数 据, show( )显示时间数据。重载运算符+和- (具有返回值 ),每执行一次 +,second自增 1,执行一次-,second自减 1。second和 minute 的值在 059 区间循环 (满 59后再自增则归 0,minute 加 1; second为 0 时再自减则为 59, minute 减 1)。 hour 的值在 023 区间循环。 源代码如下:#include using namespace std

15、;class Time private: int hour; int minute; int second;public:Time(int=0,int=0,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

16、 m,int s)if(s59)s%=60; m=m+s/60; if(m59) m%=60;h=h+m /60;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%=6

17、0; if(minute59)hour=hour+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

18、 Time:show()couthour:minute:second59) minute=minute+second/60; second%=60;if(minute59) hour=hour+minute /60; minute%=60;if(hour23) hour%=24;return *this;Time& Time:operator-(int)second-;if(second0)minute-; second=59; if(minute0) minute=59; hour-; if(hour0)hour=0; return *this;int main()Time t;int x,y,z;char temp;coutx;ciny;cinz;t.set(x,y,z);cout 现在时间是: ;t.show();do cout输入 *重新设置计数器; endl; cout输入 +计数器递加,输入 -计数器递减; endl; cout输入字母 o 计数器清零; endl;cout 输入 q 退出计数器。 endl; cout;cintemp; swit

温馨提示

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

评论

0/150

提交评论