版权说明:本文档由用户提供并上传,收益归属内容提供方,若内容存在侵权,请进行举报或认领
文档简介
1、 实验四:多态性一、实验目的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对象为参数,调用该函数时能返
2、回两个复数对象相加操作;(3)定义成员函数print,调用该函数时,以格式“real+imag i”输出当前对象的实部和虚部,例如:对象的实部和虚部分别是4.2和6.5,则调用print函数输出格式为:4.2+6.5 i;(4)编写主程序,计算出复数对象c1和c2相加结果,并将其结果输出。#include<iostream>Using namespace std;class Complexpublic:Complex()real = 0;imag = 0;Complex(double r,double i)real = r;imag = i;friend Complex opera
3、tor+(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();getch
4、ar();2、编写一个程序,其中设计一个时间类Time,用来保存时、分、秒等私有数据成员,通过重载操作符“+”实现两个时间的相加。要求将小时范围限制在大于等于0,分钟范围限制在059分,秒钟范围限制在059秒。提示:时间类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<io
5、stream.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
6、)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
7、,5,6);t2.disptime();(t1+t2).disptime();3、用友元运算符函数或成员运算符函数,重载运算符“+”、“-”、“*”,实现对实验二中实现的矩阵类的对象的加、减、乘法。#include<iostream>using namespace std;class Matrixprivate: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-&g
8、t;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<&l
9、t;"乘?不?了¢?"<<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<<
10、"加¨®不?了¢?"<<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 inta;row = r;col = c;void print()for(int i=1;i<=row;i+)for(int j=1;j&
11、lt;=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);
12、/向集合中添加一个元素 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 sMA
13、X;/存放集合中的元素 ;#include<iostream>using namespace std;class Set public: Set(int l = 0) len = l; ; void input(int d) slen = d; len+; int length() return len; int getd(int i) return si-1; void display() for (int i = 0;i<len;i+) cout<<si<<endl; Set operator+(Set s1) Set res(0); int fl
14、ag = 1; for(int i=0;i<len;i+) res.input(si); for(int j=0;j<s1.len;j+) flag = 1; for (i = 0;i<len;i+) if(s1.sj=si) flag = 0; if(flag) res.input(s1.sj); 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.sj=si)
15、flag = 0; if(flag) input(s1.sj); for(int j=0;j<s1.len;j+) flag = 1; for (int i = 0;i<len;i+) if(s1.si=sj) flag = 0; if(flag) input(sj); ; Set operator*(Set s1) for(int j=0;j<s1.len;j+) int flag = 0; for (int i = 0;i<len;i+) if(s1.sj=si) flag = 1; if(flag) input(s1.sj); Set operator=(Set
16、s1) len = 0; for(int i = 0;i<s1.len;i+)input(s1.getd(i); protected: int len; int s20; ;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()(co
17、ut<<"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-Clas
18、s 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 Bird
19、Fly; pBascpBird; cout<<"nBirdFly->n" pBase->Fly(); DragonFly * pDragon=new DragonFly; pBase=pDragon; cout<<"nDragonFly->n" pBase->Fly(); PlaneFly * pPlane=new PlaneFly; pBasepPlane; cout<<"nPlaneFly->n" pBase->Fly(); return 0, (3) 将Ba
20、seFly:Fly()声明为virtual,在main()中定义BaseFly的指针:*pBase,依次分别指向UirdFly、DragonFly和P1aneFly,并调用各类的Fly()函数,体会虚函数作用。 BaseFly * pBasenew BaseFly; BirdFly *pBird=new BirdFly;pBasepBird;程序代码:#include<iostream>using namespace std;class BaseFly public: void Fly()cout<<"n-CIass BaseFly:Fly()-n"
21、 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
22、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
23、=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_
24、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:pu
温馨提示
- 1. 本站所有资源如无特殊说明,都需要本地电脑安装OFFICE2007和PDF阅读器。图纸软件为CAD,CAXA,PROE,UG,SolidWorks等.压缩文件请下载最新的WinRAR软件解压。
- 2. 本站的文档不包含任何第三方提供的附件图纸等,如果需要附件,请联系上传者。文件的所有权益归上传用户所有。
- 3. 本站RAR压缩包中若带图纸,网页内容里面会有图纸预览,若没有图纸预览就没有图纸。
- 4. 未经权益所有人同意不得将文件中的内容挪作商业或盈利用途。
- 5. 人人文库网仅提供信息存储空间,仅对用户上传内容的表现方式做保护处理,对用户上传分享的文档内容本身不做任何修改或编辑,并不能对任何下载内容负责。
- 6. 下载文件中如有侵权或不适当内容,请与我们联系,我们立即纠正。
- 7. 本站不保证下载资源的准确性、安全性和完整性, 同时也不承担用户因使用这些下载资源对自己和他人造成任何形式的伤害或损失。
最新文档
- 中铁工程局项目经理的招聘条件及发展前景介绍
- 中国移动网络优化工程师面试全记录
- 航空公司空勤综合文员面试技巧
- 祖国有我演讲稿小学作文
- 2025年AI艺术生成技术的跨学科研究与应用
- 2026年部编版八年级道德与法治上册期末必考重难点专练(选择题100题)
- 解读校园文化演讲稿
- 校园生活演讲稿双人
- 团结拼搏运动会演讲稿
- 我的信仰演讲稿英语初中
- 人工智能通识 课件 模块6 赋能职场数据洞察:AI助力
- 林业法规与执法实务课件
- 技术投标培训课件
- 2025年海关总署公务员面试模拟题集及答案解析
- HDPE分子量分布调控对产品性能的影响及工艺优化
- 基于节约里程法的配送路径优化-以化州市A物流公司为例
- 2024苏州工业职业技术学院单招《语文》高分题库附参考答案详解【B卷】
- 买房指南课程讲解
- 儿童暴发性心肌炎诊治专家建议解读 6
- 网络空间安全法律法规解读(第二版) 课件 第1-4章 网络空间安全政策法律法规- 网络空间安全知识产权保护法律法规
- 牛肝菌产研一体化生产基地项目可行性研究报告模板-立项备案
评论
0/150
提交评论