ImageVerifierCode 换一换
格式:DOCX , 页数:14 ,大小:141.35KB ,
资源ID:3663644      下载积分:3 金币
快捷下载
登录下载
邮箱/手机:
温馨提示:
快捷下载时,用户名和密码都是您填写的邮箱或者手机号,方便查询和重复下载(系统自动生成)。 如填写123,账号就是123,密码也是123。
特别说明:
请自助下载,系统不会自动发送文件的哦; 如果您已付费,想二次下载,请登录后访问:我的下载记录
支付方式: 支付宝    微信支付   
验证码:   换一换

加入VIP,免费下载
 

温馨提示:由于个人手机设置不同,如果发现不能下载,请复制以下地址【https://www.bdocx.com/down/3663644.html】到电脑端继续下载(重复下载不扣费)。

已注册用户请登录:
账号:
密码:
验证码:   换一换
  忘记密码?
三方登录: 微信登录   QQ登录  

下载须知

1: 本站所有资源如无特殊说明,都需要本地电脑安装OFFICE2007和PDF阅读器。
2: 试题试卷类文档,如果标题没有明确说明有答案则都视为没有答案,请知晓。
3: 文件的所有权益归上传用户所有。
4. 未经权益所有人同意不得将文件中的内容挪作商业或盈利用途。
5. 本站仅提供交流平台,并不能对任何下载内容负责。
6. 下载文件中如有侵权或不适当内容,请与我们联系,我们立即纠正。
7. 本站不保证下载资源的准确性、安全性和完整性, 同时也不承担用户因使用这些下载资源对自己和他人造成任何形式的伤害或损失。

版权提示 | 免责声明

本文(C++上机实验报告 实验六.docx)为本站会员(b****3)主动上传,冰豆网仅提供信息存储空间,仅对用户上传内容的表现方式做保护处理,对上载内容本身不做任何修改或编辑。 若此文所含内容侵犯了您的版权或隐私,请立即通知冰豆网(发送邮件至service@bdocx.com或直接QQ联系客服),我们立即给予删除!

C++上机实验报告 实验六.docx

1、C+上机实验报告 实验六实验六 多态性1.实验目的1.掌握运算符重载的方法2.学习使用虚函数实现动态多态性2.实验要求1.定义Point类,有坐标_x,_y两个成员变量;对Point类重载“”(自增)、“”(自减)运算符,实现对坐标值的改变。2.定义一个车(vehiele)基类,有Run、Stop等成员函数,由此派生出自行车(bicycle)类、汽车(motorcar)类,从bicycle和motorcar派生出摩托车(motorcycle)类,它们都有Run、Stop等成员函数。观察虚函数的作用。3. (选做)对实验4中的People类重载“”运算符和“=”运算符,“”运算符判断两个peop

2、le类对象的id属性是否相等;“=”运算符实现People类对象的赋值操作。3.实验内容及实验步骤1.编写程序定义Point类,在类中定义整型的私有成员变量_x_y,定义成员函数Point& operator+();Point operator+(int);以实现对Point类重载“+”(自增)运算符,定义成员函数Point operator();Point operator(int);以实现对Point类重载“”(自减)运算符,实现对坐标值的改变。程序名:1ab8_1cpp。2.编写程序定义一个车(vehicle)基类,有Run、Stop等成员函数,由此派生出自行车(bicycle)类、汽车

3、(motorcar)类,从bicycle和motorcar派生出摩托车(motorcycle)类,它们都有Run、Stop等成员函数。在main()函数中定义vehicle、bicycle、motorcar、motorcycle的对象,调用其Run()、Stop()函数,观察其执行情况。再分别用vehicle类型的指针来调用这几个对象的成员函数,看看能否成功;把Run、Stop定义为虚函数,再试试看。程序名:lab8_2cpp。4.思考题1.如何将一个运算符重载为类的成员函数?函数类型 operator 运算符(形参表) 函数体;2.如何将一个运算符重载为类的友元函数?friend 函数类型

4、operator 运算符(形参表) 函数体;3.如何实现运行时刻的多态?在基类的成员函数前加上virtual,就可以在它的派生类中声明相同名字和类型的成员函数,在运行过程中,系统会自动判断并调用相应类中的成员函数,从而在调用过程中实现多态。5.源程序1.lab8_1.cpp#includeusing namespace std;class Pointprivate: int _x; int _y;public: /构造.析构函数 Point() Point(int,int); Point() /+.-重载 Point& operator +(); Point operator +(int);

5、Point& operator -(); Point operator -(int); /输出点坐标 void showPoint();Point:Point(int x,int y) _x=x; _y=y;Point& Point:operator +() _x+; _y+; return *this;Point Point:operator +(int) Point p=*this; +(*this); return p;Point& Point:operator -() _x-; _y-; return *this;Point Point:operator -(int) Point p=

6、*this; -(*this); return p;void Point:showPoint() coutThe point is (_x,_y)endl;int main() Point apoint(3,5); apoint.showPoint(); (apoint+).showPoint();/测试后置+ apoint.showPoint(); (+apoint).showPoint();/测试前置+ apoint.showPoint(); (apoint-).showPoint();/测试后置- apoint.showPoint(); (-apoint).showPoint();/测试

7、前置- apoint.showPoint(); return 0;2.lab8_2.cpp#includeusing namespace std; class Vehiclepublic: /基类的成员函数为虚函数 virtual void run()coutVehicle is running!endl; virtual void stop()coutVehicle is stopping!endl;class Bicycle: virtual public Vehicle/按虚基类继承public: void run()coutBicycle is running!endl; void s

8、top()coutBicycle is stopping!endl;class Motorcar:virtual public Vehicle/按虚基类继承public: void run()coutMotorcar is running!endl; void stop()coutMotorcar is stopping!endl;class Motorcycle:public Bicycle,public Motorcarpublic: void run()coutMotorcycle is running!endl; void stop()coutMotorcycle is stoppin

9、g!run(); p-stop(); p=&bic; p-run(); p-stop(); p=&mot; p-run(); p-stop(); p=&m; p-run(); p-stop(); return 0;3.lab8_3#include#includeusing namespace std;/Date类class Dateprivate: int year; int month; int day;public: Date(); Date(int y,int m,int d); Date(Date &p); Date(); void setDate(); void showDate()

10、;/People类,其中含Date类型的数据class Peopleprivate: char name11; char number7; char sex3; Date birthday;public: char id16; People(); People(char* n,char* nu,char* s,Date b,char* i); People(People &p); People(); bool operator =(People&); People& operator =(People&); void setName(); void setNumber(); void setS

11、ex(); void setId(); void showPeople();/Date构造函数Date:Date()Date:Date(int y,int m,int d) year=y; month=m; day=d; Date:Date(Date &p) year=p.year; month=p.month; day=p.day;/析构inline Date:Date()/Date成员函数,设置出生年月日void Date:setDate() int y,m,d; couty; coutm; coutd; year=y; month=m; day=d; /Date内联成员函数,输出年月日i

12、nline void Date:showDate() coutBirthday is year年month月day日endl; /People构造函数People:People();People:People(char* n,char* nu,char* s,Date b,char* i) strcpy(name,n); strcpy(number,nu); strcpy(sex,s); birthday=b; strcpy(id,i);People:People(People &p) strcpy(name,p.name); strcpy(number,p.number); birthday

13、=p.birthday; strcpy(id,p.id);/People析构inline People:People()/People成员函数,设置各类数据void People:setName() coutPlease input the persons name:; cin.getline(name,11,n);void People:setNumber() coutInput number:; cin.getline(number,7,n);void People:setSex() coutInput sex:; cin.getline(sex,3,n);void People:setI

14、d() coutInput id:; cin.getline(id,16,n);/People内联成员函数,输出人员信息inline void People:showPeople() coutName:nameendl; coutNumber:numberendl; coutSex:sexendl; coutID:idendl;bool People:operator =(People& p) return id=p.id;People& People:operator =(People& p) strcpy(name,p.name); strcpy(number,p.number); bir

15、thday=p.birthday; strcpy(id,p.id); return *this;/检测=重载的普通函数void test(People& x,People& y) if(strcmp(x.id,y.id)=0) coutTheirs IDs are sameendl; else coutTheirs IDs are differentendl; int main() /*int i; char spaceA; /生成3个Date类型的对象 Date date3=Date(0,0,0),Date(0,0,0),Date(0,0,0); /生成3个People类型的对象 Peopl

16、e person3=People(0,0,0,date0,0),People(0,0,0,date1,0),People(0,0,0,date2,0); /设置这3个对象的各类信息 for(i=0;i3;i+) personi.setName(); personi.setNumber(); personi.setSex(); personi.setId(); datei.setDate(); spaceA=getchar(); /输出这3个对象的各类信息 for(i=0;i3;i+) personi.showPeople(); datei.showDate(); */ /测试=重载 Date

17、d1(0,0,0); People p1(lizhibo,01,male,d1,123456); People p2(wangyusen,02,male,d1,123); People p3(zhouhao,03,male,d1,123456); test(p1,p2); test(p1,p3); /测试=重载 coutBefore =endl; p1.showPeople(); p1=p3; coutAfter =endl; p1.showPeople(); return 0;6.运行结果1. 2.直接使用对象.函数的形式可以成功调用函数:使用基类指针后出现错误,只能调用基类的成员函数:将基类的成员函数设置成虚函数之后,成功实现调取各个派生类的成员函数:其中在使用Vehicle类型指针指向Motorcycle类型的对象时会出现错误:将Vehicle按照虚基类继承,问题解决。3.7.心得体会学习了解了多态,通过编写程序,进行上机练习,学会了如何利用虚函数实现程序的多态性,进行函数的重载;而且学会了如何对各类运算符进行重载,使得各类运算符满足同类之间的运算,使得程序更加高效;还学会了使用基类指针或引用对基类的派生类中的各类重载函数进行调用,收获很大。

copyright@ 2008-2022 冰豆网网站版权所有

经营许可证编号:鄂ICP备2022015515号-1