《C++第四次实验.doc》由会员分享,可在线阅读,更多相关《C++第四次实验.doc(23页珍藏版)》请在taowenge.com淘文阁网|工程机械CAD图纸|机械工程制图|CAD装配图下载|SolidWorks_CaTia_CAD_UG_PROE_设计图分享下载上搜索。
1、Four short words sum up what has lifted most successful individuals above the crowd: a little bit more.-author-dateC+第四次实验天津理工大学天津理工大学计算机与通信工程学院实验报告2013 至 2014 学年 第 二 学期课程名称高级语言程序设计实验( 4 )实验名称多态程序设计实验时间 2014 年 4 月 28 日 第 3 节 至 第 6 节学号姓名专业信息安全主讲教师唐召东辅导教师唐召东软件环境VC+6硬件环境PC机实验目的(1)掌握运算符重载的方法;(2)掌握使用虚函数实
2、现动态多态性;实验内容(应包括实验题目、实验要求、实验任务等)二、练习项目:1.定义Point类,有坐标x,y两个成员变量,利用友元函数对Point类重载“+”运算符,实现对坐标值的改变。具体要求如下:(1) 编写程序定义Point类,在类中定义整型的私有成员变量x,y;(2) 在类中定义两个友元函数,分别重载前置+和后置+;(3) 编写主函数测试。注意函数有无返回值的区别,以及返回值是否带有&应用符号。2.定义Point类,有坐标x,y两个成员变量,利用运算符重载对Point类重载“+”运算符,实现对坐标值的改变。具体要求如下:(1) 编写程序定义Point类,在类中定义整型的私有成员变量x
3、,y;(2) 定义成员函数Point& operator+(); Point operator+(int);以实现对Point类重载“+”运算符,分别重载前置+和后置+;(3) 编写主函数测试。3.定义一个分数类,通过重载运算符实现分数的四则运算、求负运算和赋值运算。其中,要求加法“+” 和减法“-”用友元函数实现重载,其他运算符用成员函数实现重载。4.编写程序,定义抽象基类Container,由此派生出2个派生类球体类Sphere,圆柱体类Cylinder,分别用虚函数分别计算表面积和体积。(1) 球体的表面积为:,球体的体积为; (2) 圆柱表面积为: 2R(h+R) 圆柱体的体积R2h。
4、定义相应的对象,编写主函数测试。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区间循环。实验过程与实验结果(可包括实验实施的步骤、算法描述、流程、结论等)1. 源代码:#include using namespace std;clas
5、s 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+(
6、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.源代码:#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;
7、 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.源代
8、码:#include #include 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 operat
9、or-(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
10、 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
11、?_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.源代码:#include using namespace std;const double PI=3.14;class Containerprivate:double r;double h;public:Container
12、()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(
13、)*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
14、=&a;coutSphere s=s() v=v()endl;p=&b;coutCylinder s=s() v=v()endl;return 0;输出结果:5.源代码:#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 getminut
15、e();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
16、=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
17、,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-;se
18、cond=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;cintemp;switch(temp)case *:coutx;ciny;cinz;t.set(x,y,z);co
19、ut现在时间是:;t.show();break;case +:t+;cout现在时间是:;t.show();break;case -:t-;cout现在时间是:;t.show();break;case o:t.set(0,0,0);cout现在时间是:;t.show();break;case q:break;default:cout请输入正确的数据!getname()=sub_m-getname()*sub=*sub_m;sub_m+;sub_m-=num_m;sub+;sub-=shumu;void Cstudent:insert_s() /录入分数int temp;coutPlease i
20、nput the scores.n;for(int i=0;ishumu;i+)coutgetname():temp;sub-setscore(temp);sub+;sub-=shumu;void Cstudent:dill()int sum=0;double c=0.0,s=0.0;for(int i=0;igetscore()=95 & sub-getscore()setpoint(4.5);else if(sub-getscore()=90 & sub-getscore()setpoint(4.0);else if(sub-getscore()=85 & sub-getscore()se
21、tpoint(3.5);else if(sub-getscore()=80 & sub-getscore()setpoint(3.0);else if(sub-getscore()=75 & sub-getscore()setpoint(2.5);else if(sub-getscore()=70 & sub-getscore()setpoint(2.0);else if(sub-getscore()=65 & sub-getscore()setpoint(1.5);else if(sub-getscore()=60 & sub-getscore()setpoint(1.0);else sub
22、-setpoint(0.0);sub-setflag(true);sum=sum+sub-getscore();c=c+sub-getxuefen()*sub-getpoint();s=s+sub-getxuefen();sub+;sub-=shumu; ave_g=double(sum)/shumu;ave_p=c/s;void Cstudent:print_h()coutID Name sex class date average of score average of pointn;cout-n;void Cstudent:print()coutid name sex cls date
23、ave_g ave_pendl;coutThe score of each subject:n;coutName score pointendl;cout-n;for(int i=0;ishumu;i+)coutgetname() getscore() getpoint()endl;sub+;sub-=shumu;coutThe retest subject is:n;int key=0;for(int i=0;igetflag()=true)key=1;coutgetname()endl;sub+;sub-=shumu;if(key=0) cout nonen; int main() Cstudent s,x;s.fun();s.insert_b();s.select();s.insert_s();s.dill();x.insert_b();x.select();x.insert_s();x.dill();s.print_h();s.print();x.print();return 0;附录(可包括源程序清单或其它说明)