东北大学C++实验报告_第1页
东北大学C++实验报告_第2页
东北大学C++实验报告_第3页
东北大学C++实验报告_第4页
东北大学C++实验报告_第5页
免费预览已结束,剩余1页可下载查看

下载本文档

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

文档简介

1、.实验六1. 实验要求(1)定义 Point 类,有坐标 _x ,_y 两个成员变量; 对 Point 类重载 “ ”(自增 )、“ ”(自减 )运算符,实现对坐标值的改变。(2)定义一个车 (vehiele)基类,有 Run、Stop 等成员函数,由此派生出自行车 (bicycle)类、汽车 (motorcar)类,从 bicycle 和 motorcar 派生出摩托车 (motorcycle)类,它们都有 Run、Stop 等成员函数。观察虚函数的作用。2. 实验内容及实验步骤(1) 编写程序定义 Point 类,在类中定义整型的私有成员变量 _x_y,定义成员函数 Point& oper

2、ator+();Point operator+(int);以实现对 Point 类重载 “+”(自增 )运算符,定义成员函数 Point operator ();Point operator (int) ;以实现对 Point 类重载“”(自减 )运算符,实现对坐标值的改变。程序名:1ab8_1cpp。(2) 编写程序定义一个车 (vehicle)基类,有 Run、Stop 等成员函数,由此派生出自行车 (bicycle)类、汽车 (motorcar)类,从 bicycle 和 motorcar 派生出摩托车(motorcycle)类,它们都有 Run、Stop 等成员函数。 在 main()

3、函数中定义 vehicle、bicycle、motorcar、motorcycle 的对象,调用其 Run()、Stop()函数,观察其执行情况。再分别用 vehicle 类型的指针来调用这几个对象的成员函数,看看能否成功;把 Run、Stop 定义为虚函数,再试试看。程序名: lab8_2 cpp。3. 源程序Lab8_1#includeusing namespace std;class Pointpublic:Point(int X,int Y): _x(X),_y(Y)Point operator+();Point operator+(int);Point operator-();Poi

4、nt operator-(int);void Putout() const;private:int _x,_y;.void Point:Putout() constcout(_x,_y)endl;Point Point: operator+()_x+;_y+;return *this;Point Point:operator+(int)+_x;+_y;return *this;Point Point:operator-()_x-;_y-;return *this;Point Point:operator-(int)-_x;-_y;return *this;int main()Point A(6

5、,7);coutPoint+: ;(A+).Putout();cout+Point: ;(+A).Putout();coutPoint-: ;(A-).Putout();cout-Point: ;(-A).Putout();return 0;运行结果.Lab8_21)在 main()函数中定义 vehicle、bicycle、motorcar、motorcycle 的对象,调用其Run()、 Stop()函数。#includeusing namespace std;class Vehiclepublic:void Run()cout 基类 Vehicle 的 Run 函数调用 endl; vo

6、id Stop()cout 基类 Vehicle 的 Stop 函数调用 endl;class bicycle :public Vehiclepublic:void Run()cout 派生类 bicycle 的 Run 函数调用 endl; void Stop()cout 派生类 bicycle 的 Stop 函数调用 endl;class motorcar :public Vehiclepublic:void Run()cout 派生类 motorcar 的 Run 函数调用 endl; void Stop()cout 派生类 motorcar 的 Stop 函数调用 endl;class

7、 motorcycle :public bicycle,public motorcarpublic:void Run()cout 派生类 motorcycle 的 Run 函数调用 endl; void Stop()cout 派生类 motorcycle 的 Stop 函数调用 endl;int main()Vehicle V;bicycle B;motorcar M;motorcycle Mc;V.Run();V.Stop();B.Run();B.Stop();M.Run();M.Stop();.Mc.Run();Mc.Stop();return 0;运行结果2)用 vehicle 类型的指

8、针来调用这几个对象的成员函数。#includeusing namespace std;class Vehiclepublic:virtual void Run()cout 基类 Vehicle 的 Run 函数调用 endl; virtual void Stop()cout 基类 Vehicle 的 Stop 函数调用 endl;class bicycle :virtual public Vehiclepublic:void Run()cout 派生类 bicycle 的 Run 函数调用 endl; void Stop()cout 派生类 bicycle 的 Stop 函数调用 endl;c

9、lass motorcar :virtual public Vehiclepublic:void Run()cout 派生类 motorcar 的 Run 函数调用 endl; void Stop()cout 派生类 motorcar 的 Stop 函数调用 endl;class motorcycle :public bicycle,public motorcarpublic:void Run()cout 派生类 void Stop()coutRun();motorcycle 的 Run 函数调用 endl; motorcycle 的 Stop 函数调用 Stop();int main()Vehicle V;bicycle B;motorcar M;motorcycle Mc;fun(&V

温馨提示

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

评论

0/150

提交评论