版权说明:本文档由用户提供并上传,收益归属内容提供方,若内容存在侵权,请进行举报或认领
文档简介
1、实验六 多态性1. 实验目的1.掌握运算符重载的方法2.学习使用虚函数实现动态多态性2. 实验要求1.定义Point类,有坐标_x,_y两个成员变量;对Point类重载“”(自增)、“”(自减)运算符,实现对坐标值的改变。2.定义一个车(vehiele)基类,有Run、Stop等成员函数,由此派生出自行车(bicycle)类、汽车(motorcar)类,从bicycle和motorcar派生出摩托车(motorcycle)类,它们都有Run、Stop等成员函数。观察虚函数的作用。3. (选做)对实验4中的People类重载“”运算符和“=”运算符,“”运算符判断两个people类对象的id属性
2、是否相等;“=”运算符实现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)类、汽车(moto
3、rcar)类,从bicycle和motorcar派生出摩托车(motorcycle)类,它们都有Run、Stop等成员函数。在main()函数中定义vehicle、bicycle、motorcar、motorcycle的对象,调用其Run()、Stop()函数,观察其执行情况。再分别用vehicle类型的指针来调用这几个对象的成员函数,看看能否成功;把Run、Stop定义为虚函数,再试试看。程序名:lab8_2cpp。4. 思考题1. 如何将一个运算符重载为类的成员函数?函数类型 operator 运算符(形参表) 函数体;2. 如何将一个运算符重载为类的友元函数?friend 函数类型 op
4、erator 运算符(形参表) 函数体;3.如何实现运行时刻的多态?在基类的成员函数前加上virtual,就可以在它的派生类中声明相同名字和类型的成员函数,在运行过程中,系统会自动判断并调用相应类中的成员函数,从而在调用过程中实现多态。5. 源程序1. lab8_1.cpp#include<iostream>using namespace std;class Pointprivate: int _x; int _y;public: /构造.析构函数 Point() Point(int,int); Point() /+.-重载 Point& operator +(); Poi
5、nt operator +(int); 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;Poin
6、t Point:operator -(int) Point p=*this; -(*this); return p;void Point:showPoint() cout<<"The point is ("<<_x<<","<<_y<<")"<<endl;int main() Point apoint(3,5); apoint.showPoint(); (apoint+).showPoint();/测试后置+ apoint.showPoint(); (+apoin
7、t).showPoint();/测试前置+ apoint.showPoint(); (apoint-).showPoint();/测试后置- apoint.showPoint(); (-apoint).showPoint();/测试前置- apoint.showPoint(); return 0;2. lab8_2.cpp#include<iostream>using namespace std; class Vehiclepublic: /基类的成员函数为虚函数 virtual void run()cout<<"Vehicle is running!&quo
8、t;<<endl; virtual void stop()cout<<"Vehicle is stopping!"<<endl;class Bicycle: virtual public Vehicle/按虚基类继承public: void run()cout<<"Bicycle is running!"<<endl; void stop()cout<<"Bicycle is stopping!"<<endl;class Motorcar:virtua
9、l public Vehicle/按虚基类继承public: void run()cout<<"Motorcar is running!"<<endl; void stop()cout<<"Motorcar is stopping!"<<endl;class Motorcycle:public Bicycle,public Motorcarpublic: void run()cout<<"Motorcycle is running!"<<endl; void st
10、op()cout<<"Motorcycle is stopping!"<<endl;int main() Vehicle veh; Bicycle bic; Motorcar mot; Motorcycle m; /对象名.函数测试 /*veh.run(); veh.stop(); bic.run(); bic.stop(); mot.run(); mot.stop(); m.run(); m.stop();*/ /基类指针测试 Vehicle* p; p=&veh; p->run(); p->stop(); p=&bic
11、; p->run(); p->stop(); p=&mot; p->run(); p->stop(); p=&m; p->run(); p->stop(); return 0;3. lab8_3#include<iostream>#include<cstring>using namespace std;/Date类class Dateprivate:int year;int month;int day;public:Date();Date(int y,int m,int d);Date(Date &p);Dat
12、e();void setDate();void showDate();/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
13、setName();void setNumber();void setSex();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;cout<<"Input the year:&
14、quot;cin>>y;cout<<"Input the month:"cin>>m;cout<<"Input the day:"cin>>d;year=y;month=m;day=d;/Date内联成员函数,输出年月日inline void Date:showDate()cout<<"Birthday is "<<year<<"年"<<month<<"月"<<da
15、y<<"日"<<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,);strcpy(number,p.number);birthday=p.birthday;strcpy(id,p.id);/Peop
16、le析构inline People:People()/People成员函数,设置各类数据void People:setName()cout<<"Please input the person's name:"cin.getline(name,11,'n');void People:setNumber()cout<<"Input number:"cin.getline(number,7,'n');void People:setSex()cout<<"Input sex:&
17、quot;cin.getline(sex,3,'n');void People:setId()cout<<"Input id:"cin.getline(id,16,'n');/People内联成员函数,输出人员信息inline void People:showPeople()cout<<"Name:"<<name<<endl;cout<<"Number:"<<number<<endl;cout<<"
18、Sex:"<<sex<<endl;cout<<"ID:"<<id<<endl;bool People:operator =(People& p)return id=p.id;People& People:operator =(People& p)strcpy(name,);strcpy(number,p.number);birthday=p.birthday;strcpy(id,p.id);return *this;/检测=重载的普通函数void test(People
19、& x,People& y)if(strcmp(x.id,y.id)=0)cout<<"Their's IDs are same"<<endl;elsecout<<"Their's IDs are different"<<endl;int main()/*int i;char spaceA;/生成3个Date类型的对象Date date3=Date(0,0,0),Date(0,0,0),Date(0,0,0);/生成3个People类型的对象People person3=Pe
20、ople("0","0","0",date0,"0"),People("0","0","0",date1,"0"),People("0","0","0",date2,"0");/设置这3个对象的各类信息for(i=0;i<3;i+)personi.setName();personi.setNumber();personi.setSex();personi.setId();datei.setDate();spaceA=getchar();/输出这3个对象的各类信息for(i=0;i<3;i+)personi.showPeople();datei.showDate();*/测试=重载Date d1(0,0,0);People p1("lizhibo"
温馨提示
- 1. 本站所有资源如无特殊说明,都需要本地电脑安装OFFICE2007和PDF阅读器。图纸软件为CAD,CAXA,PROE,UG,SolidWorks等.压缩文件请下载最新的WinRAR软件解压。
- 2. 本站的文档不包含任何第三方提供的附件图纸等,如果需要附件,请联系上传者。文件的所有权益归上传用户所有。
- 3. 本站RAR压缩包中若带图纸,网页内容里面会有图纸预览,若没有图纸预览就没有图纸。
- 4. 未经权益所有人同意不得将文件中的内容挪作商业或盈利用途。
- 5. 人人文库网仅提供信息存储空间,仅对用户上传内容的表现方式做保护处理,对用户上传分享的文档内容本身不做任何修改或编辑,并不能对任何下载内容负责。
- 6. 下载文件中如有侵权或不适当内容,请与我们联系,我们立即纠正。
- 7. 本站不保证下载资源的准确性、安全性和完整性, 同时也不承担用户因使用这些下载资源对自己和他人造成任何形式的伤害或损失。
最新文档
- 中药入库验收规范制度
- 保安迎宾制度规范标准
- 科室6s工作制度规范
- 公司厂区停车制度规范
- 城运调度大厅制度规范
- 名医诊区工作规范制度
- 重庆市规范办学行为制度
- 仓管员工管理制度规范
- 教师看书打卡制度规范
- 中层干部请示制度规范
- 肝内胆管恶性肿瘤护理查房
- 河南省省直辖县级行政区划济源市2024-2025学年八年级(上)期末物理试卷(含解析)
- 四川省医疗护理员考试题库及答案
- 物流新人开票培训
- 食品现场品鉴活动方案
- 护理管理学课程教学大纲
- 2025-2026学年浙教版(2023)初中信息科技七年级上册教学计划及进度表
- 昆明医科大学海源学院《高等数学下》2024-2025学年第一学期期末试卷
- 中国特发性面神经麻痹(面瘫)治疗指南(2022)解读
- 威海平改坡管理办法
- 心内科病例讨论与分析
评论
0/150
提交评论