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

下载本文档

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

文档简介

实验六实验目的1.掌握运算符重载的方法2.学习使用虚函数实现动态多态性实验要求1.定义Point类,有坐标_x,_y两个成员变量;对Point类重载“”(自增)、“”(自减)运算符,实现对坐标值的改变。2.定义一个车(vehiele)基类,有Run、Stop等成员函数,由此派生出自行车(bicycle)类、汽车(motorcar)类,从bicycle和motorcar派生出摩托车(motorcycle)类,它们都有Run、Stop等成员函数。观察虚函数的作用。3. (选做)对实验4中的People类重载“”运算符和“”运算符,“”运算符判断两个people类对象的id属性是否相等;“”运算符实现People类对象的赋值操作。实验内容及实验步骤1. 编写程序定义Point类,在类中定义整型的私有成员变量_x_y,定义成员函数Point& operator+();Point operator+(int);以实现对Point类重载“+”(自增)运算符,定义成员函数Point operator();Point operator(int);以实现对Point类重载“”(自减)运算符,实现对坐标值的改变。程序名:1ab8_1Cpp#include using namespace std;class Pointpublic:Point();Point(int x,int y);Point() /Point类析构函数Point& operator+(); /公有成员函数Point operator+(int);Point&operator-();Point operator-(int);void Show();private:int _x; /私有数据成员 int _y;Point:Point() /Point类构造函数_x=0;_y=0;Point:Point(int x,int y) /Point类构造函数_x=x;_y=y;Point & Point:operator+() /重载后置+运算符为Point类成员函数_x+;_y+;Point Point:operator+(int) /重载前置+运算符为Point类成员函数Point old=*this;+(this-_x);+(this-_y);return old;Point & Point:operator-() /重载后置-运算符为Point类成员函数_x-;_y-;Point Point:operator-(int) /重载前置-运算符为Point类成员函数Point old=*this;-(this-_x);-(this-_y);return old;void Point:Show() /输出Point的坐标值 cout_x,_y)endl;int main()Point a(2,3); /定义一个Point类对象aPoint b=a+; /定义Point类对象b并用a+初始化bPoint c=+a; /定义Point类对象c并用+a初始化cPoint d=-a; /定义Point类对象d并用-a初始化dPoint e=a-; /定义Point类对象e并用a-初始化ecoutPoint a(;a.Show(); /输出a的坐标coutPoint b(;b.Show(); /输出b的坐标coutPoint c(;c.Show(); /输出c的坐标coutPoint d(; d.Show(); /输出d的坐标coutPoint e(;e.Show(); /输出e的坐标return 0;运行结果:2编写程序定义一个车(vehicle)基类,有Run、Stop等成员函数,由此派生出自行车(bicycle)类、汽车(motorcar)类,从bicycle和motorcar派生出摩托车(motorcycle)类,它们都有Run、Stop等成员函数。在main()函数中定义vehicle、bicycle、motorcar、motorcycle的对象,调用其Run()、Stop()函数,观察其执行情况。再分别用vehicle类型的指针来调用这几个对象的成员函数,看看能否成功;把Run、Stop定义为虚函数,再试试看。程序名:lab8_2cpp#include using namespace std;class vehicle /基类vehiclepublic:vehicle()vehicle()virtual void Run()coutThe vehicle is running!endl; /定义虚函数Run()virtual void Stop()coutThe vehicle has stopped!endl; /定义虚函数Stop();class bicycle: virtual public vehicle /定义派生类bicycle,声明基类为派生类的虚基类public:void Run()coutThe bicycle is running!endl;void Stop()coutThe bicycle has stopped!endl;bicycle()bicycle();class motorcar: virtual public vehicle /定义派生类motorcar,声明基类为派生类的虚基类public:void Run()coutThe motorcar is running!endl;void Stop()coutThe motorcar has stopped!endl;motorcar()motorcar();class motorcycle:public bicycle,public motorcar /以bicycle类和motorcar类作为基类派生新类motorcyclepublic:void Run()coutThe motorcycle is running!endl;void Stop()coutThe motorcycle has stopped!Run(); /通过指针调用vehicle类成员函数p-Stop();p=&b; /使指针p指向bicycle类对象bp-Run(); /通过指针调用bicycle类成员函数p-Stop();p=&c; /使指针p指向motorcar类对象cp-Run(); /通过指针调用motorcar类成员函数p-Stop();p=&d; /使指针指向motorbicycle类对象dp-Run(); /通过指针调用motorcycle类成员函数p-Stop(); return 0;运行结果:3. (选做)对实验4中的People类重载“”运算符和“”运算符,“”运算符判断两个people类对象的id属性是否相等;“”运算符实现People类对象的赋值操作。源程序:#include #include using namespace std;class Birthdaypublic: Birthday(int Year,int Month,int Day); /构造函数 Birthday() /构造函数 Birthday() /析构函数 Birthday(Birthday &p); /复制构造函数 int showBirthday() coutyear年month月day日; /内联成员函数 int enter();private:int year,month,day;Birthday:Birthday(Birthday &p)year=p.year;month=p.month;day=p.day;Birthday:Birthday(int Year,int Month,int Day) /Birthday类构造函数 year=Year; month=Month; day=Day; int Birthday:enter() coutyearmonthday;class people /定义people类public:people() /people类构造函数people() /people类析构函数people(people &p);people operator=(people &);people operator-(people &);int show();int enter();private:string number,id,sex; /字符串类型变量数据成员 Birthday p1; /Birthday类数据成员;int people:show() coutn性别 sex 编号 ; coutnumber; cout 生日 ; p1.showBirthday(); /调用Birthday类成员函数 cout 身份证号 idendl;int people:enter() p1.enter(); coutsex; coutnumber; coutid;people:people(people &p):p1(p.p1) /people类复制构造函数number=p.number;sex=p.sex; id=p.id;people people:operator=(people &V) /重载=运算符成员函数 if(id=V.id) cout have the same id! endl; else cout have different id!endl;people people:operator-(people &U) /重载-运算符成员函数 number=U.number; /使用字符串赋值运算符 sex=U.sex; id=U.id; p1=U.p1; return *this; /返回this指针int main()int t;people p2; /定义对象数组p2for(t=0;t2;t+) /输入对象数组成员信息cout输入第t+1个人员的信息endl;pt.enter();for(t=0;t2;t+) /输出对象数组成员信息coutn第t+1个人员信息如下:endl;pt.show();people p3; /定义people类的对象p3p3-p1; /使用重载运算符将p1赋给p3coutn第3个人员信息如下:endl;p3.show();coutp0,p1; p0=p1; /使用重载运算符-判断p0和p1的id是否相等return 0;运行结果:思考题1. 如何将一个运算符重载为类的成员函数?一般语法形式:返回类型 operator 运算符(形参表)函数体函数的参数个数比原来的曹祖数个数要少一个(后置“+”,“-”除外)。2. 如何将一个运算符重载为类的友元函数?一般形式:friend 返回类型 operator 运算符(形参表)函数体运算所需的操作数都需要通过函数的形参表传递,在形参表中形参从左至右的顺序

温馨提示

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

评论

0/150

提交评论