收藏 分销(赏)

实验四-多态性.docx

上传人:精**** 文档编号:2277620 上传时间:2024-05-24 格式:DOCX 页数:19 大小:76.07KB 下载积分:8 金币
下载 相关 举报
实验四-多态性.docx_第1页
第1页 / 共19页
实验四-多态性.docx_第2页
第2页 / 共19页


点击查看更多>>
资源描述
______________________________________________________________________________________________________________ 实验四:多态性 一、实验目的 1、掌握运算符重载的基本方法。 2、掌握友元运算符函数和成员运算符函数的使用方法及两者之间的不同。 3、学习虚函数的定义与使用方法。 4、了解静态多态性和动态多态性。 5、学习使用虚函数和继承实现动态多态性。 二、试验内容1、编写一个程序,要求: (1)生明一个类Complex(复数类),定义类Complex的两个对象c1和c2,对象c1通过构造函数直接指定复数的实部和虚部(类私有数据成员为double类型:real和imag)为2.5及3.7,对象c2通过构造函数直接指定复数的实部和虚部为4.2及6.5; (2)定义友元运算符重载函数,它以c1、c2对象为参数,调用该函数时能返回两个复数对象相加操作; (3)定义成员函数print,调用该函数时,以格式“real+imag i”输出当前对象的实部和虚部,例如:对象的实部和虚部分别是4.2和6.5,则调用print函数输出格式为:4.2+6.5 i; (4)编写主程序,计算出复数对象c1和c2相加结果,并将其结果输出。 #include<iostream> Using namespace std; class Complex { public: Complex() { real = 0; imag = 0; } Complex(double r,double i) { real = r; imag = i; } friend Complex operator+(Complex a,Complex b) { Complex c(a.real+b.real,a.imag+b.imag); return c; } void print() { cout<<this->real<<"+ i"<<this->imag<<endl; } private: double real,imag; }; void main() { Complex c1(2.5,3.7); c1.print(); Complex c2(4.2,6.5); c2.print(); Complex c3 = c1+c2; c3.print(); getchar(); } 2、编写一个程序,其中设计一个时间类Time,用来保存时、分、秒等私有数据成员,通过重载操作符“+”实现两个时间的相加。要求将小时范围限制在大于等于0,分钟范围限制在0~59分,秒钟范围限制在0~59秒。 提示:时间类Time的参考框架如下: class Time { public: Time(int h=0,int m=0,int s=0);//构造函数 Time operator+(Time &);//运算符重载函数,实现两个时间的相加 void disptime();//显示时间函数 private: int hours,minutes,seconds; }; 代码: #include<iostream.h> class Time { public: Time(int h=0,int m=0,int s=0)//构造函数 { hours = h; minutes = m; seconds = s; }; Time operator+(Time &a)//运算符重载函数,实现两个时间的相加 { Time t(this->hours+a.hours,this->minutes+a.minutes,this->seconds+a.seconds); if(t.seconds>59) { t.seconds-=60; t.minutes+=1; } if(t.minutes>59) { t.minutes-=60; t.hours+=1; } return t; }; void disptime()//显示时间函数 { cout<<this->hours<<"小时"<<this->minutes<<"分钟"<<this->seconds<<"秒"<<endl; }; private: int hours,minutes,seconds; }; void main() { Time t1(1,2,3); t1.disptime(); Time t2(4,5,6); t2.disptime(); (t1+t2).disptime(); } 3、用友元运算符函数或成员运算符函数,重载运算符“+”、“-”、“*”,实现对实验二中实现的矩阵类的对象的加、减、乘法。 #include<iostream> using namespace std; class Matrix { private: int row,col; int* m; public: int &operator()(int i,int j) { return m[(i-1)*this->col+j-1]; }; Matrix operator-(Matrix &a) { if(a.row!=this->row||a.col!=this->col) { cout<<"减?不?了¢?"<<endl; return a; } Matrix result(row,col); for(int i=1;i<=row;i++) for(int j=1;j<=col;j++) { result(i,j) = this->m[(i-1)*col+j-1]-a(i,j); } return result; }; Matrix operator*(Matrix &a) { if(a.row!=this->row||a.col!=this->col) { cout<<"乘?不?了¢?"<<endl; return a; } Matrix result(a.row,col); for(int i=1;i<=row;i++) for(int j=1;j<=col;j++) { int temp = 0; for(int k =1;k<col;k++) temp = this->m[(i-1)*col+k-1]*a(k,j); } return result; }; Matrix operator+(Matrix &a) { if(a.row!=this->row||a.col!=this->col) { cout<<"加¨®不?了¢?"<<endl; return a; } Matrix result(row,col); for(int i=1;i<=row;i++) for(int j=1;j<=col;j++) { result(i,j) = this->m[(i-1)*col+j-1]+a(i,j); } return result; }; Matrix(int r,int c) { int a = r*c; m = new int[a]; row = r; col = c; }; void print() { for(int i=1;i<=row;i++) { for(int j=1;j<=col;j++) { cout<<m[(i-1)*col+j-1]<<" "; } cout<<endl; } } }; 4、编写一个程序,用于进行集合的和、并和交运算。例如输入整数集合{9,5,4,3,6,7}和{2,4,6,9},计算出他们进行集合的并、差和交运算后的结果。 【提示】 (1)可以用一下表达式实现整数集合的基本运算: s1+s2 两个整数集合的并运算 s1-s2 两个整数集合的差运算 s1*s2 两个整数集合的交运算 (2)参考以下Set类的框架,用于完成集合基本运算所需的各项功能。 class Set { public: Set(); void input(int d);//向集合中添加一个元素 int length();//返回集合中的元素个数 int getd(int i);//返回集合中位置i的元素 void display();//显示集合的所有元素 Set operator+(Set s1);//成员运算符重载函数,实现集合的并运算 Set operator-(Set s1);//成员运算符重载函数,实现集合的差运算 Set operator*(Set s1);//成员运算符重载函数,实现集合的交运算 Set operator=(Set s1);//成员运算符重载函数,实现集合的赋值运算 protected: int len;//统计结合中元素的个数; int s[MAX];//存放集合中的元素 }; #include<iostream> using namespace std; class Set { public: Set(int l = 0) { len = l; }; void input(int d) { s[len] = d; len++; } int length() { return len; } int getd(int i) { return s[i-1]; } void display() { for (int i = 0;i<len;i++) { cout<<s[i]<<endl; } } Set operator+(Set s1) { Set res(0); int flag = 1; for(int i=0;i<len;i++) { res.input(s[i]); } for(int j=0;j<s1.len;j++) { flag = 1; for (i = 0;i<len;i++) if(s1.s[j]==s[i]) flag = 0; if(flag) res.input(s1.s[j]); } return res; }; Set operator-(Set s1) { Set res(); int flag = 0; for(int j=0;j<s1.len;j++) { flag = 1; for (int i = 0;i<len;i++) if(s1.s[j]==s[i]) flag = 0; if(flag) input(s1.s[j]); } for(int j=0;j<s1.len;j++) { flag = 1; for (int i = 0;i<len;i++) if(s1.s[i]==s[j]) flag = 0; if(flag) input(s[j]); } }; Set operator*(Set s1) { for(int j=0;j<s1.len;j++) { int flag = 0; for (int i = 0;i<len;i++) if(s1.s[j]==s[i]) flag = 1; if(flag) input(s1.s[j]); } } Set operator=(Set s1) { len = 0; for(int i = 0;i<s1.len;i++) input(s1.getd(i)); } protected: int len; int s[20]; }; void main() { Set a; a. operator=(Set s1); a. operator*(Set s1); a. operator+(Set s1); a. operator-(Set s1); } 5、下面提供一个体会继承中的多态性和虚函数在多态性中的作用的题目。请根据提示进行实验。定义类BaseFly,其中Fly()函数输出特定内容。例如: class BaseFly { public: void Fly()(cout<<"\n-----CIass BaseFly::Fly()-----\n";} }; (1)定义类BirdFly、DragonFly和PlaneFly,都继承自BaseFly,重载Fly()函数,使得各类中的Fly()函数分别输出不同的内容。 class BirdFly:public BaseFly { public: void Fly() {cout<<"\n-----class BirdFly::Fly()-----\n";} }; class DragonFly:public BaseFly { public: vold Fly(){cout<<"\n-----Class DragonFly::Fly()-----\n";) ); class PlaneFly:public BaseFly { public: void Fly(){cout<<"\n-----Class PlaneFly::Fly()-----\n";} }; (2)在main()函数中,用“new”关键字分配出以上四个类的实例,调用各个实例的Fly()函数测试多态性。请参考以下代码: int main() { cout<<"\n测试继承中的多态性(Virtual? Or not?):\n'; BaseFly *pBase; BirdFly *pBird=new BirdFly; pBasc=pBird; cout<<"\nBirdFly->\n"; pBase->Fly(); DragonFly * pDragon=new DragonFly; pBase=pDragon; cout<<"\nDragonFly->\n"; pBase->Fly(); PlaneFly * pPlane=new PlaneFly; pBase=pPlane; cout<<"\nPlaneFly->\n"; pBase->Fly(); return 0, } (3) 将BaseFly::Fly()声明为virtual,在main()中定义BaseFly的指针:*pBase,依次分别指向UirdFly、DragonFly和P1aneFly,并调用各类的Fly()函数,体会虚函数作用。 BaseFly * pBase=new BaseFly; BirdFly *pBird=new BirdFly; pBase=pBird; 程序代码: #include<iostream> using namespace std; class BaseFly { public: void Fly(){cout<<"\n-----CIass BaseFly::Fly()-----\n";} } class BirdFly:public BaseFly { public: void Fly() {cout<<"\n-----class BirdFly::Fly()-----\n";} }; class DragonFly:public BaseFly { public: void Fly(){cout<<"\n-----Class DragonFly::Fly()-----\n";} }; class PlaneFly:public BaseFly { public: void Fly(){cout<<"\n-----Class PlaneFly::Fly()-----\n";} }; int main() { cout<<"\n测试继承中的多态性(Virtual? Or not?):\n"; BaseFly *pBase; BirdFly *pBird=new BirdFly; pBase=pBird; cout<<"\nBirdFly->\n"; pBase->Fly(); DragonFly * pDragon=new DragonFly; pBase=pDragon; cout<<"\nDragonFly->\n"; pBase->Fly(); PlaneFly * pPlane=new PlaneFly; pBase=pPlane; cout<<"\nPlaneFly->\n"; pBase->Fly(); BaseFly *pBase=new BaseFly; BirdFly *pBird=new BirdFly; pBase=pBird; return 0; } 6、写一个程序,定义抽象类Container: class Container { protected: double radius; public: Container(double r);//抽象类Container的构造函数 virtual double surface_area()=0;//纯虚函数surface_area virtual double volume()=0;//纯虚函数volume }; 【要求】 建立3个继承Container的派生类:Sphere(球体)、Cylinder(圆柱体)、Cube(正方体),让每一个派生类都包含虚函数surface_area()和volume(),分别用来球体、圆柱体和正方体的表面积和体积。要求写出主程序,应用C++的多态性,分别计算边长为6.0的正方体、半径为5.0的球体,以及半径为5.0和高为6.0的圆柱体的表面积和体积。 #include<iostream> using namespace std; class Container { protected: double radius; public: Container() { } Container(double r)//抽象类Container的构造函数 { radius = r; } virtual double surface_area()=0;//纯虚函数surface_area virtual double volume()=0;//纯虚函数volume }; class Sphere:public Container { public: Sphere(double r) { radius = r; } double surface_area() { cout<<4*radius*radius*3.14<<endl; return 0; } double volume() { cout<<4/3*radius*radius*radius*3.14<<endl; return 0; } }; class Cylinder:public Container { private: double height; public: Cylinder(double r,double h) { radius = r; height = h; } double surface_area() { cout<<(2*radius*radius*3.14)+(2*radius*3.14*height)<<endl; return 0; } double volume() { cout<<height*radius*radius*3.14<<endl; return 0; } }; class Cube:public Container { public: Cube(double r) { radius = r; } double surface_area() { cout<<radius*radius*6<<endl; return 0; } double volume() { cout<<radius*radius*radius<<endl; return 0; } }; void main() { Cube a(6.0); a.surface_area(); a.volume(); Sphere b(5.0); b.surface_area(); b.volume(); Cylinder c(5.0,6.0); c.surface_area(); c.volume(); } Welcome To Download !!! 欢迎您的下载,资料仅供参考! 精品资料
展开阅读全文

开通  VIP会员、SVIP会员  优惠大
下载10份以上建议开通VIP会员
下载20份以上建议开通SVIP会员


开通VIP      成为共赢上传

当前位置:首页 > 考试专区 > 中考

移动网页_全站_页脚广告1

关于我们      便捷服务       自信AI       AI导航        抽奖活动

©2010-2026 宁波自信网络信息技术有限公司  版权所有

客服电话:0574-28810668  投诉电话:18658249818

gongan.png浙公网安备33021202000488号   

icp.png浙ICP备2021020529号-1  |  浙B2-20240490  

关注我们 :微信公众号    抖音    微博    LOFTER 

客服