免费预览已结束,剩余10页可下载查看
下载本文档
版权说明:本文档由用户提供并上传,收益归属内容提供方,若内容存在侵权,请进行举报或认领
文档简介
天津理工大学实验报告学院(系)名称:计算机与通信工程学院姓名 学号 专业计算机科学与技术班级教学二班实验项目实验四 多态程序设计课程名称高级程序设计语言II课程代码0667026实验时间2016年11月11日 第7、8节实验地点计算机软件实验室7-215批改意见成绩教师签字: 实验目的:(1)理解类和对象的概念;(2)掌握类与对象的定义方法;(3)理解类的成员的访问控制的含义,公有和私有成员的区别;(4)掌握构造函数和析构函数的含义与作用、定义方式和实现;(5)能够根据给定的要求定义类并实现类的成员函数;(6)掌握string类的使用方法(7)了解C+面向对象程序设计的基本思想、基本方法和基本步骤;(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 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 operator(ostream &out,const Point &p)outPoint(p.x,p.y)n;friend void operator+(Point &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);couta;coutb;a+;+b;couta;coutb;return 0;运行结果如下:2. 定义Point类,有坐标x,y两个成员变量,利用运算符重载对Point类重载“+”运算符,实现对坐标值的改变。具体要求如下:(1) 编写程序定义Point类,在类中定义整型的私有成员变量x,y;(2) 定义成员函数Point& operator+(); Point operator+(int);以实现对Point类重载“+”运算符,分别重载前置+和后置+;(3) 编写主函数测试。源代码如下:#include using namespace std;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 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. 定义一个分数类,通过重载运算符实现分数的四则运算、求负运算和赋值运算。其中,要求加法“+” 和减法“-”用友元函数实现重载,其他运算符用成员函数实现重载。代码:#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 Fenshu& 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 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:print()if(zi=0)cout0mu?zi: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,圆柱体类Cylinder,分别用虚函数分别计算表面积和体积。(1) 球体的表面积为:,球体的体积为; 圆柱表面积为: 2R(h+R) 圆柱体的体积R2h。(2) 定义相应的对象,编写主函数测试。代码如下:#include using namespace std;const double PI=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)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(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()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;class Timeprivate: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 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%=60;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 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();docout输入*重新设置计数器;endl;cout输入+计数器递加,输入-计数器递减;endl;cout输入字母o计数器清零;endl;cout输入q退出计数器。endl;cout;cint
温馨提示
- 1. 本站所有资源如无特殊说明,都需要本地电脑安装OFFICE2007和PDF阅读器。图纸软件为CAD,CAXA,PROE,UG,SolidWorks等.压缩文件请下载最新的WinRAR软件解压。
- 2. 本站的文档不包含任何第三方提供的附件图纸等,如果需要附件,请联系上传者。文件的所有权益归上传用户所有。
- 3. 本站RAR压缩包中若带图纸,网页内容里面会有图纸预览,若没有图纸预览就没有图纸。
- 4. 未经权益所有人同意不得将文件中的内容挪作商业或盈利用途。
- 5. 人人文库网仅提供信息存储空间,仅对用户上传内容的表现方式做保护处理,对用户上传分享的文档内容本身不做任何修改或编辑,并不能对任何下载内容负责。
- 6. 下载文件中如有侵权或不适当内容,请与我们联系,我们立即纠正。
- 7. 本站不保证下载资源的准确性、安全性和完整性, 同时也不承担用户因使用这些下载资源对自己和他人造成任何形式的伤害或损失。
最新文档
- 《GB-T 37406-2019电子封装用球形二氧化硅微粉球形度的检测方法 颗粒动态光电投影法》专题研究报告
- 师德考核个人总结(幼儿教师版二)
- 建筑幕墙设计师创新应用评优考核试卷含答案
- 职业培训师诚信强化考核试卷含答案
- 脂肪酸酰化及酯化操作工岗前实操效果考核试卷含答案
- 纹版复制工创新意识强化考核试卷含答案
- 硅油及乳液生产工安全知识宣贯能力考核试卷含答案
- 城市轨道交通设备调度员岗位应急处置技术规程
- 有机介质电容器纸、膜切割工岗前技术应用考核试卷含答案
- 平板显示膜回收工岗前强化考核试卷含答案
- 杞柳编织课件
- ip形象设计合同范本
- 乙型肝炎病毒实验室检测技术规范(2025年修订版)(征求意见稿)
- 泵管垂直固定钢管架施工方案
- 疼痛科医生进修成果汇报
- ISO 9001(DIS)-2026《质量管理体系-要求》之10:“6策划-6.1应对风险和机遇的措施-6.1.2应对风险的措施”专业深度解读和应用指导材料雷泽佳编写20
- 易制爆安全管理培训制度课件
- 2025电化学储能电站施工及验收规范
- 设备技术改造合同范本
- 福建省泉州市2024-2025学年高二上学期期末教学质量监测政治试卷(含答案)
- 大型设备安全操作规程全集
评论
0/150
提交评论