东北大学C++实验报告.doc_第1页
东北大学C++实验报告.doc_第2页
东北大学C++实验报告.doc_第3页
东北大学C++实验报告.doc_第4页
东北大学C++实验报告.doc_第5页
已阅读5页,还剩3页未读 继续免费阅读

下载本文档

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

文档简介

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

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

3、员函数。在 main() 函数中定义 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);Poin

4、t operator-();Point 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

5、 main()Point A(6,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

6、 的 Run函数调用 endl;void 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 mo

7、torcycle :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;class motorcar :virtu

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

温馨提示

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

评论

0/150

提交评论