欢迎来到天天文库
浏览记录
ID:38717865
大小:126.00 KB
页数:6页
时间:2019-06-18
《湖北理工(黄石理工)C++实验 实验三多态性》由会员上传分享,免费在线阅读,更多相关内容在教育资源-天天文库。
1、实验3多态性实验课程名:面向对象程序设计(C++)专业班级:09计科(一)班学号:2********姓名:*****实验时间:12.6实验地点:K4-102指导教师:祁文青一、实验目的和要求(1)了解多态性的概念。(2)掌握运算符重载的基本方法。(3)掌握虚函数的定义和使用方法。(4)掌握纯虚函数和抽象类的概念和用法。二、实验内容1.分析并调试下列程序,写出程序的输出结果,并解释输出结果。#includeusingnamespacestd;classB{public:virtualvoidf1(doublex){cou
2、t<<"B::f1(double)"<3、){cout<<"D::f3(double)"<<3*x<f1(1.23);pb->f1(1.23);pb->f2(1.23);pb->f3(1.23);pb->f3(3.14);return0;}程序的运行结果。答:2.编写一个程序,其中设计一个时间类Time,用来保存时、分、秒等私有数据成员,通过重载操作符“+”实现两个时间的相加。要求将小时范围限制在大于等于0,分钟范围限制在0~59,秒钟范围限制在0~59秒。【提示】时间类Time{public:T4、ime(inth=0,intm=0,ints=0);Timeoperator+(Time&);voiddisptime(string);private:inthourse;intminutes;intseconds;};实验代码:#include#includeusingnamespacestd;classTime{public:Time(inth=0,intm=0,ints=0);Timeoperator+(Time&);voiddisptime(string);private:inthourse;i5、ntminutes;intseconds;};Time::Time(inth,intm,ints){hourse=h;minutes=m;seconds=s;}TimeTime::operator+(Time&t1){Timep;p.seconds=seconds+t1.seconds;inttemp=p.seconds;p.seconds=temp%60;p.minutes=minutes+t1.minutes+temp/60;temp=p.minutes;p.minutes=temp%60;p.hourse=hourse+t1.hou6、rse+temp/60;returnp;}voidTime::disptime(strings){cout<>hh>>mm>>ss;}while(hh<07、8、mm<09、10、mm>5911、12、ss<013、14、ss>59);Timet(hh,mm,ss);do{cout<<"输入第二个时间时分秒(例如23042)"<>15、hh>>mm>>ss;}while(hh<016、17、mm<018、19、mm>5920、21、ss<022、23、ss>59);TimeT(hh,mm,ss),t_total;t_total=t+T;t_total.disptime("输出结果(时/分/秒):");return0;}程序运行结果:3.给出下面的抽象基类container;classcontainer{protected:doubleradius;public:container(doubleradius1);virtualdoublesurface_area()=0;virtualdoublevol24、ume()=0;};要求建立3个继承container的派生类cube、sphere与cylinder,让每一个派生类都包含虚函数surface_area()和volume(),分别用来计算正
3、){cout<<"D::f3(double)"<<3*x<f1(1.23);pb->f1(1.23);pb->f2(1.23);pb->f3(1.23);pb->f3(3.14);return0;}程序的运行结果。答:2.编写一个程序,其中设计一个时间类Time,用来保存时、分、秒等私有数据成员,通过重载操作符“+”实现两个时间的相加。要求将小时范围限制在大于等于0,分钟范围限制在0~59,秒钟范围限制在0~59秒。【提示】时间类Time{public:T
4、ime(inth=0,intm=0,ints=0);Timeoperator+(Time&);voiddisptime(string);private:inthourse;intminutes;intseconds;};实验代码:#include#includeusingnamespacestd;classTime{public:Time(inth=0,intm=0,ints=0);Timeoperator+(Time&);voiddisptime(string);private:inthourse;i
5、ntminutes;intseconds;};Time::Time(inth,intm,ints){hourse=h;minutes=m;seconds=s;}TimeTime::operator+(Time&t1){Timep;p.seconds=seconds+t1.seconds;inttemp=p.seconds;p.seconds=temp%60;p.minutes=minutes+t1.minutes+temp/60;temp=p.minutes;p.minutes=temp%60;p.hourse=hourse+t1.hou
6、rse+temp/60;returnp;}voidTime::disptime(strings){cout<>hh>>mm>>ss;}while(hh<0
7、
8、mm<0
9、
10、mm>59
11、
12、ss<0
13、
14、ss>59);Timet(hh,mm,ss);do{cout<<"输入第二个时间时分秒(例如23042)"<>
15、hh>>mm>>ss;}while(hh<0
16、
17、mm<0
18、
19、mm>59
20、
21、ss<0
22、
23、ss>59);TimeT(hh,mm,ss),t_total;t_total=t+T;t_total.disptime("输出结果(时/分/秒):");return0;}程序运行结果:3.给出下面的抽象基类container;classcontainer{protected:doubleradius;public:container(doubleradius1);virtualdoublesurface_area()=0;virtualdoublevol
24、ume()=0;};要求建立3个继承container的派生类cube、sphere与cylinder,让每一个派生类都包含虚函数surface_area()和volume(),分别用来计算正
此文档下载收益归作者所有