C++上机实验报告实验六_第1页
C++上机实验报告实验六_第2页
C++上机实验报告实验六_第3页
已阅读5页,还剩5页未读 继续免费阅读

下载本文档

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

文档简介

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

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

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

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

5、tor+(int);Point&operator-();Pointoperator-(int);/输出点坐标voidshowPoint();Point:Point(intx,inty)_x=x;_y=y;Point&Point:operator+()(_x+;_y+;return*this;PointPoint:operator+(int)(Pointp=*this;+(*this);returnp;Point&Point:operator-()(_x-;_y-;return*this;PointPoint:operator-(int)Pointp=*this;-(*t

6、his);returnp;voidPoint:showPoint()(cout<<"Thepointis("<<_x<<”,"<<_y<<”)”<<endl;intmain()(Pointapoint(3,5);apoint.showPoint();(apoint+).showPoint();/测试后置+apoint.showPoint();(+apoint).showPoint();/测试前置+apoint.showPoint();(apoint-).showPoint();/测试后置-ap

7、oint.showPoint();(-apoint).showPoint();/测试前置-apoint.showPoint();return0;lab8_2.cpp#include<iostream>usingnamespacestd;classVehiclepublic:/基类的成员函数为虚函数virtualvoidrun()cout<<"Vehicleisrunning!"<<endl;virtualvoidstop()cout<<"Vehicleisstopping!"<<endl;cla

8、ssBicycle:virtualpublicVehicle/按虚基类继承public:voidrun()cout<<"Bicycleisrunning!"<<endl;voidstop()cout<<"Bicycleisstopping!"<<endl;classMotorcar:virtualpublicVehicle/按虚基类继承public:voidrun()cout<<"Motorcarisrunning!"<<endl;voidstop()(cout&

9、lt;<"Motorcarisstopping!"<<endl;;classMotorcycle:publicBicycle,publicMotorcar(public:voidrun()(cout<<"Motorcycleisrunning!"<<endl;voidstop()(cout<<"Motorcycleisstopping!"<<endl;intmain()(Vehicleveh;Bicyclebic;Motorcarmot;Motorcyclem;/对象名

10、.函数测试/*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;p->run();p->stop();p=&mot;p->run();p->stop();p=&m;p->run();p->stop();return0;lab8_3#include<iostream>#include<cst

11、ring>usingnamespacestd;/Date类classDate(private:intyear;intmonth;intday;public:Date();Date(inty,intm,intd);Date(Date&p);Date();voidsetDate();voidshowDate();/People类,其中含Date类型的数据classPeople(private:charname11;charnumber7;charsex3;Datebirthday;public:charid16;People();People(char*n,char*nu,char*

12、s,Dateb,char*i);People(People&p);People();booloperator=(People&);People&operator=(People&);voidsetName();voidsetNumber();voidsetSex();voidsetId();voidshowPeople();/Date构造函数Date:Date()Date:Date(inty,intm,intd)year=y;month=m;day=d;Date:Date(Date&p)(year=p.year;month=p.month;day=p.d

13、ay;/析构inlineDate:Date()/Date成员函数,设置出生年月日voidDate:setDate()inty,m,d;cout<<"Inputtheyear:"cin>>y;cout<<"Inputthemonth:"cin>>m;cout<<"Inputtheday:"cin>>d;year=y;month=m;day=d;/Date内联成员函数,输出年月日inlinevoidDate:showDate()(cout<<"B

14、irthdayis"<<year<<"年"<<month<<"月"<<day<<"日"<<endl;/People构造函数People:People();People:People(char*n,char*nu,char*s,Dateb,char*i)strcpy(name,n);strcpy(number,nu);strcpy(sex,s);birthday=b;strcpy(id,i);People:People(People&p

15、)strcpy(name,);strcpy(number,p.number);birthday=p.birthday;strcpy(id,p.id);/People析构inlinePeople:People()/People成员函数,设置各类数据voidPeople:setName()cout<<"Pleaseinputtheperson'sname:"cin.getline(name,11,'n');voidPeople:setNumber()cout<<"Inputnumber:"cin.g

16、etline(number,7,'n');voidPeople:setSex()cout<<"Inputsex:"cin.getline(sex,3,'n');voidPeople:setId()(cout<<"Inputid:"cin.getline(id,16,'n');/People内联成员函数,输出人员信息inlinevoidPeople:showPeople()(cout<<"Name:"<<name<<endl;co

17、ut<<"Number:"<<number<<endl;cout<<"Sex:"<<sex<<endl;cout<<"ID:"<<id<<endl;boolPeople:operator=(People&p)(returnid=p.id;People&People:operator=(People&p)(strcpy(name,);strcpy(number,p.number);birthda

18、y=p.birthday;strcpy(id,p.id);return*this;/检测=重载的普通函数voidtest(People&x,People&y)(if(strcmp(x.id,y.id)=0)(cout<<"Their'sIDsaresame"<<endl;else(cout<<"Their'sIDsaredifferent"<<endl;intmain()(/*inti;charspaceA;/生成3个Date类型的对象Datedate3=Date(0,0,0

19、),Date(0,0,0),Date(0,0,0);/生成3个People类型的对象Peopleperson3=People("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()

20、;/输出这3个对象的各类信息for(i=0;i<3;i+)personi.showPeople();datei.showDate();*/测试=重载Dated1(0,0,0);Peoplep1("lizhibo”,"01”,"male”,d1,”123456");Peoplep2("wangyusen”,"02”,"male”,d1,"123”);Peoplep3("zhouhao","03","male",d1,"123456"

21、);test(p1,p2);test(p1,p3);/测试=重载cout<<"Before="<<endl;p1.showPeople();p1=p3;cout<<"After="<<endl;p1.showPeople();return0;6.运行结果1.Thepointis<3,5>Thepointis<3,5>Thepointis<4,6>Thepointis<5,7>Thepointis<5,7>Thepointis5,7)Thepoint

22、is4,6)Thepointis<3,5>Thepointis<3,5>Processreturned0<0x0>executiontine:0.218sPressanykeytocontinue«2.直接使用对象.函数的形式可以成功调用函数:Uehicleisrunning'!Uehicleisstopping!Bicj/cleisrunning!Bicycleisstopping!Motorcarisrunning*Motorcarisstopping?Motorcycleisrunning*Motorcycleisstopping使用基类指针后出现错误,只能调用基类的成员函数:ehicleisehicleisehicleisehicleisehicleisehicleisehicleisehicleisrunn

温馨提示

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

评论

0/150

提交评论