付费下载
下载本文档
版权说明:本文档由用户提供并上传,收益归属内容提供方,若内容存在侵权,请进行举报或认领
文档简介
1、实验六1.实验要求(1)定义Point类,有坐标_x,_y两个成员变量;对Point类重载“ ”(自增)、“”(自减)运算符,实现对坐标值的改变。(2)定义一个车(vehiele)基类, 有Run、Stop等成员函数, 由此派生出自 行车(bicycle)类、汽车(motorcar)类,从bicycle和motorcar派生出摩托车(motorcycle)类,它们都有Run、Stop等成员函数。观察虚函数的作用。2.实验内容及实验步骤(1)编写程序定义Point类,在类中定义整型的私有成员变量_x_y,定义成 员函数Point& operator+();Point operator+(
2、int);以实现对Point类重 载“+”(自增)运算符,定义成员函数Pointoperator();Point operator(int);以实现对Point类重载“”(自减)运算符,实现对坐 标值的改变。程序名:1 ab8_1cpp。(2)编写程序定义一个车(vehicle)基类,有Run、Stop等成员函数,由此 派生出自行车(bicycle)类、汽车(motorcar)类,从bicycle和motorcar派生出 摩托车(motorcycle)类,它们都有Run、Stop等成员函数。在main()函数中定 义vehicle、bicycle、motorcar、motorcycle的对象,
3、调用其Run()、Stop()函数,观察其执行情况。 再分别用vehicle类型的指针来调用这几个对象的成员 函数,看看能否成功;把RunStop定义为虚函数,再试试看。程序名:Iab8_2 .cpp。3.源程序Lab8_1#include using namespace std;class Pointpublic:Point(int X,int Y): _x(X),_y(Y)Point operator+();Point operator+(int);Point operator-();Point operator-(int);void Putout() const;private:int
4、_x,_y;void Point:Putout() const cout(_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 Poi nt:operator-(i nt)-_x;-_y;return *this;int mai n()Poi nt A(6,7); cout Poi nt+:;(A+).Putout(); cout+Poi nt:
5、;(+A).Putout(); coutPoi nt-:;(A-).Putout(); cout |Mfll卜 T .Lab8_21) 在main()函数中定义vehicle、bicycle、motorcar、motorcycle调用其Run()、Stop()函数。#in cludeusing n amespace std;class Vehiclepublic:void Run()cout基类Vehicle的Run函数调用endl;void Stop()cout基类Vehicle的Stop函数调用endl;class bicycle :public Vehiclepublic:void Ru
6、n()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 motorcycle :public bicycle,public motorcar public:void Run()cout派生类motorcycle的Run函数调用endl;void Stop()cout
7、派生类motorcycle的Stop函数调用endl;;int mai n()Vehicle V;bicycle B;motorcar M;motorcycle Mc;V.Run(); V.Stop();B.Ru n(); B.Stop();M.R un(); M.Stop();Mc.R un(); Mc.Stop();return 0;运行结果2)用vehicle类型的指针来调用这几个对象的成员函数#in cludeusing namespace std;class Vehiclepublic:virtual void Run()cout基类Vehicle的Run函数调用endl;virtu
8、al 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 :virtual 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函数调用Ru n();p-Stop();int mai n()Vehicle V;bicycle B;motorcar M;motorcycle Me; fun(&V);fun(&B);fun(&M);fun(
温馨提示
- 1. 本站所有资源如无特殊说明,都需要本地电脑安装OFFICE2007和PDF阅读器。图纸软件为CAD,CAXA,PROE,UG,SolidWorks等.压缩文件请下载最新的WinRAR软件解压。
- 2. 本站的文档不包含任何第三方提供的附件图纸等,如果需要附件,请联系上传者。文件的所有权益归上传用户所有。
- 3. 本站RAR压缩包中若带图纸,网页内容里面会有图纸预览,若没有图纸预览就没有图纸。
- 4. 未经权益所有人同意不得将文件中的内容挪作商业或盈利用途。
- 5. 人人文库网仅提供信息存储空间,仅对用户上传内容的表现方式做保护处理,对用户上传分享的文档内容本身不做任何修改或编辑,并不能对任何下载内容负责。
- 6. 下载文件中如有侵权或不适当内容,请与我们联系,我们立即纠正。
- 7. 本站不保证下载资源的准确性、安全性和完整性, 同时也不承担用户因使用这些下载资源对自己和他人造成任何形式的伤害或损失。
最新文档
- 工程监理实务工程师考核试卷及答案解析
- 2026年容器化机密项目公司成立分析报告
- 2026年早教百宝箱项目公司成立分析报告
- 2026年宠物临终关怀项目公司成立分析报告
- 2025年苏州工业园区职业技术学院辅导员考试真题
- 2026年通信工程师专业认证题录
- 2026年英文四级考试写作与翻译模拟题
- 2026年教育心理测试与应用技能专业认证题集
- 2026年网络安全技术认证考试模拟题
- 宁夏沙湖旅游股份有限公司招聘笔试真题2025
- 十五五地下综合管廊智能化运维管理平台建设项目建设方案
- 户外领队培训课件
- 2026年及未来5年中国饲料加工设备行业发展前景预测及投资战略研究报告
- JJF 1218-2025标准物质研制报告编写规则
- 一次函数-经典趣题探究
- 骨科老年护理课件
- 加装电梯业主反对协议书
- 人教版(2024)七年级上册地理第1~6章共6套单元测试卷汇编(含答案)
- 物流公司消防安全管理制度
- 北魏《元桢墓志》完整版(硬笔临)
- 肺奴卡菌病课件
评论
0/150
提交评论