实验13-虚函数(共20页)_第1页
实验13-虚函数(共20页)_第2页
实验13-虚函数(共20页)_第3页
实验13-虚函数(共20页)_第4页
实验13-虚函数(共20页)_第5页
已阅读5页,还剩15页未读 继续免费阅读

下载本文档

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

文档简介

1、精选优质文档-倾情为你奉上实验13 虚函数牛旭艳 智能二班 一、实验目的1、复习运算符重载。2、了解虚基类的作用和用法。3、进一步理解多态性的概念。4、了解虚函数的作用及使用方法。5、了解静态关联和动态关联的概念和用法。6、了解纯虚函数和抽象类的概念和用法。二、实验内容1.定义Point类,有坐标x,y两个成员变量,利用友元函数对Point类重载“+”运算符,实现对坐标值的改变。具体要求如下:(1)编写程序定义Point类,在类中定义整型的私有成员变量x,y;(2)在类中定义两个友元函数,分别重载前置+和后置+;(3)编写主函数测试。注意函数有无返回值的区别,以及返回值是否带有&应用符

2、号。2.定义Point类,有坐标x,y两个成员变量,利用成员函数对Point类重载“+”运算符,实现对坐标值的改变。具体要求如下:(1)编写程序定义Point类,在类中定义整型的私有成员变量x,y;(2)定义成员函数Point operator+(); Point operator+(int);以实现对Point类重载“+”运算符,分别重载前置+和后置+;(3)编写主函数测试。3.定义一个分数类,通过重载运算符实现分数的四则运算、求负运算和赋值运算。其中,要求加法“+” 和减法“-”用友元函数实现重载,其他运算符用成员函数实现重载。4.定义一个车(vehicle)基类,有Run、Stop等成员

3、函数,由此派生出自行车(bicycle)类、汽车(motorcar)类,从bicycle和motorcar派生出摩托车(motorcycle)类,它们都有Run、Stop等成员函数。具体要求如下:(1)编写程序定义一个车(vehicle)基类,有Run、Stop等成员函数;(2)由车(vehicle)基类派生出自行车(bicycle)类、汽车类(motorcar),从bicycle类和motorcar类派生出摩托车类(motorcycle),它们都有Run、Stop等成员函数。(3)在main()函数中定义vehicle、bicycle、motorcar、motorcycle的对象,调用其Ru

4、n()、Stop()函数,观察其执行情况。(4)分别用vehicle类型的指针来调用几个对象的成员函数,看看能否成功(提示:把Run、Stop定义为虚函数)。5.编写程序,定义抽象基类Container,由此派生出2个派生类球体类Sphere,圆柱体类Cylinder,分别用虚函数分别计算表面积和体积。(1)球体的表面积为:,球体的体积为; 圆柱表面积为: 2R(h+R) ,圆柱体的体积R2h。(2)定义相应的对象,编写主函数测试。6.分别声明Teacher(教师)类和Cadre(干部)类,采用多重继承方式由这两个类派生出新类Teacher_Cadre(教师兼干部)。要求:在两个基类中都包含一

5、部分相同名字的数据成员name(姓名),age(年龄)和成员函数display()。在Teacher类中还包含数据成员title(职称),在Cadre类中还包含数据成员post(职务),在Teacher_Cadre(教师兼干部)中还包含数据成员wages(工资)。在派生类Teacher_Cadre的成员函数show中调用Teacher类中的display函数,输出姓名、年龄、职称,然后再用cout语句输出职务与工资。7编写程序,定义抽象基类Shape(形状),由它派生出3个派生类:Circle(圆形) 、Rectangle(矩形)和Triangle(三角形),用虚函数Show()分别显示各种图

6、形的相关信息,并计算显示所有图形的总面积。8编写程序,定义抽象基类Shape(形状),由它派生出3个派生类: Circle(圆形)、Rectangle(矩形)和Square(正方形),用虚函数ShowArea()分别显示各种图形的面积,并计算显示所有图形的总面积。要求用基类指针数组,使它的每一个元素指向一个派生类对象。3、 实验程序及结果1、#include<iostream>using namespace std;class pointprivate:int x,y;public:point(int,int);friend void operator+(point&);f

7、riend void operator+(point&,int);void display(); ;point:point(int a=0,int b=0):x(a),y(b)void point:display()cout<<"x="<<x<<'t'<<"y="<<y;void operator+(point& A)+A.x; +A.y;void operator+(point& A,int)+A.x; +A.y;int main()point A(1,

8、2);+A;cout<<"+A之后:"A.display();A+;cout<<'n'<<"A+之后:"A.display(); return 0;2、#include<iostream>using namespace std;class pointprivate:int x,y;public:point(int,int);point& operator+();point operator+(int);void display(); ;point:point(int a=0,int

9、b=0):x(a),y(b)void point:display()cout<<"x="<<x<<'t'<<"y="<<y;point& point:operator+()+x; +y;return *this;point point:operator+(int)+x; +y;return *this;int main()point A(1,2);+A;cout<<"+A之后:"A.display();A+;cout<<'

10、;n'<<"A+之后:"A.display();cout<<'n'return 0;3、#include<iostream.h>class fenshuint fz,fm;public:fenshu(int i=0,int j=0);void huajian();void setData()cout<<"输入分子:"<<endl;cin>>fz;cout<<"输入分母:"<<endl;cin>>fm;fr

11、iend fenshu operator+(fenshu &a,fenshu &b);friend fenshu operator-(fenshu &a,fenshu &b);fenshu operator*(fenshu &a);fenshu operator/(fenshu &a);fenshu &operator=(fenshu &);void show()huajian();if(fz%fm=0)cout<<(fz/fm)<<endl;elsecout<<fz<<"

12、/"<<fm<<endl;fenshu:fenshu(int i,int j)fz=i;fm=j;void fenshu:huajian()int t;while(fm=0)cout<<"分母不能为零,请重新输入:"<<endl;setData();if(fz<=fm)t=fz;elset=fm;for(int i=2;i<=t;)if(fz%i=0&&fm%i=0) fz=fz/i; fm=fm/i;elsei+;fenshu operator+(fenshu &a,fenshu

13、 &b)fenshu temp;temp.fz=a.fz*b.fm+b.fz*a.fm;temp.fm=a.fm*b.fm;return temp;fenshu operator-(fenshu &a,fenshu &b)fenshu temp;temp.fz=a.fz*b.fm-b.fz*a.fm;temp.fm=a.fm*b.fm;return temp;fenshu fenshu:operator *(fenshu &a)fenshu temp;temp.fz=a.fz*fz;temp.fm=a.fm*fm;return temp;fenshu fensh

14、u:operator /(fenshu &a)fenshu temp;temp.fz=a.fz*fm;temp.fm=a.fm*fz;return temp;fenshu &fenshu:operator=(fenshu &a)fz=a.fz;fm=a.fm;return *this;void main()fenshu A,B,A1,A2,A3,A4,A5;A.setData();A.huajian();A.show();cout<<endl;B.setData();B.huajian();B.show();cout<<endl;A1=A+B;A

15、1.show();A2=A-B;A2.show();A3=A*B;A3.show();A4=A/B;A4.show();A5=A;A5.show();4、#include<iostream>using namespace std;class vehicledouble speed;public:vehicle(int a):speed(a)virtual void Run();virtual void Stop();void vehicle:Run()cout<<"speed="<<speed<<'t'void

16、 vehicle:Stop()cout<<"speed=1"class bicycle:virtual public vehicleprivate:double speed;public:virtual void Run();virtual void Stop();bicycle(int a):vehicle(a) speed=a;void bicycle:Run()cout<<"speed="<<speed<<'t'void bicycle:Stop()cout<<"

17、speed=2"class motorcar:virtual public vehicleprivate:double speed;public:virtual void Run();virtual void Stop();motorcar(int a):vehicle(a)speed=a;void motorcar:Run()cout<<"speed="<<speed<<'t'void motorcar:Stop()cout<<"speed=3"class motorcycle:

18、public bicycle, public motorcarprivate:double speed;public:void Run();void Stop();motorcycle(int a):vehicle(a),bicycle(a),motorcar(a)speed=a;void motorcycle:Run()cout<<"speed="<<speed<<'t'void motorcycle:Stop()cout<<"speed=4"int main() vehicle A(10

19、0),*p; bicycle AB(90); motorcar AC(80); motorcycle ABC(70);cout<<"A:"A.Run();A.Stop();cout<<'n'<<"AB:"AB.Run();AB.Stop();cout<<'n'<<"AC:"AC.Run();AC.Stop();cout<<'n'<<"ABC:" ABC.Run();ABC.Stop()

20、;cout<<'n'<<"用指针调用:"cout<<'n'<<"A:"p=&A;p->Run();p->Stop(); cout<<'n'<<"AB:"p=&AB;p->Run();p->Stop();cout<<'n'<<"AC:"p=&AC;p->Run();p->Stop(); cout<

21、;<'n'<<"ABC:"p=&ABC;p->Run();p->Stop();cout<<'n'<<"感谢使用本程序哦!"return 0;5、#include<iostream>using namespace std;class Containervirtual double area()=0;virtual double volume()=0;public:virtual void show()=0;class Sphere:public Con

22、tainerprivate:double r;public: Sphere(double a):r(a)double area()return 4*3.14*r*r;double volume()return (double)4/3*3.14*r*r*r;void show()cout<<"球面积为:"<<area()<<'n' cout<<"球体积为:"<<volume()<<'n'class Cylinder:public Containerpri

23、vate:double r,h;public: Cylinder(double a=0,double b=0):r(a),h(b)double area()return 2*3.14*r*(h+r);double volume()return 3.14*r*r*h;void show()cout<<"圆柱体面积为:"<<area()<<'n' cout<<"圆柱体体积为:"<<volume()<<'n'int main() Container *pr;

24、 Sphere A(1); pr=&A; pr->show(); Cylinder B(1,2); pr=&B; pr->show(); cout<<"感谢使用程序哦!"<<'n' return 0;6、#include<iostream>#include<string>using namespace std;class teacherprivate:string name;string title;int age;public: teacher(string N,string T,

25、int A):name(N),title(T),age(A)void display()cout<<"姓名:"<<name<<'t'<<"年龄:"<<age<<'t'<<"职称:"<<title;class Cadreprivate:string name;int age;public:string post;Cadre(string N,string P,int A):name(N),post(P),age

26、(A)void display()cout<<"职务:"<<post;class Teacher_Cadre:public teacher,public Cadre private:int wage;public: Teacher_Cadre(string N,string T,int A,string P,int W):teacher(N,T,A),Cadre(N,P,A)wage=W;void show()teacher:display();cout<<'n'<<"职务:"<<

27、;post<<'t'<<"工资:"<<wage<<'n'int main()Teacher_Cadre A("袁东志","C+",20,"校长",5000);A.show();cout<<'n'return 0;7、#include<iostream>using namespace std;class Shapepublic: virtual void show()=0;class Circle:

28、public Shapeprivate:double r;public:Circle(double a):r(a) void show() cout<<"圆的半径为:"<<r<<",面积为:"<<3.14*r*r<<'n' ;class Rectangle:public Shapeprivate:double a,b;public:Rectangle(double l,double h):a(l),b(h)void show()if(a>=b)cout<<&qu

29、ot;矩形的长为:"<<a<<",宽为:"<<b;else cout<<"矩形的长为:"<<b<<",宽为:"<<a;cout<<",面积为:"<<a*b<<'n'class Square:public Shapeprivate:double e;public:Square(double a):e(a)void show()cout<<"正方形的边长

30、为:"<<e<<",面积为:"<<e*e<<'n'int main() Shape *pr; Circle A(2.0); pr=&A; pr->show(); Rectangle B(2,3); pr=&B; pr->show(); Square C(3); pr=&C; pr->show(); cout<<'n' return 0;8、#include<iostream>using namespace std;class Shapepublic: static double sumarea; static void output() cout<<"总面积为:"<<sumarea<<'n' virtual void

温馨提示

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

评论

0/150

提交评论