




下载本文档
版权说明:本文档由用户提供并上传,收益归属内容提供方,若内容存在侵权,请进行举报或认领
文档简介
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+(int);以
2、实现对Point类重载“+”(自增)运算符,定义成员函数Point operator();Point operator(int);以实现对Point类重载“”(自减)运算符,实现对坐标值的改变。程序名:1ab8_1cpp。(2) 编写程序定义一个车(vehicle)基类,有Run、Stop等成员函数,由此派生出自行车(bicycle)类、汽车(motorcar)类,从bicycle和motorcar派生出摩托车(motorcycle)类,它们都有Run、Stop等成员函数。在main()函数中定义vehicle、bicycle、motorcar、motorcycle的对象,调用其Run()、S
3、top()函数,观察其执行情况。再分别用vehicle类型的指针来调用这几个对象的成员函数,看看能否成功;把Run、Stop定义为虚函数,再试试看。程序名:lab8_2cpp。3. 源程序Lab8_1#includeusing 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 _x,_y;void P
4、oint: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,7);coutPoint+: ;(A+).Putout();cout+Point: ;(+A).Putout()
5、;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;void Stop()cout基类Vehicle的Stop函数调用endl;class bicycle :public Vehicle pub
6、lic: 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 motorcycle :public bicycle,public motorcarpublic: void Run()cout派生类motorcycle的Run函数调用endl; void
7、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类型的指针来调用这几个对象的成员函数。#includeusing namespace std;class Vehiclepublic:virtual void Run()cout基类Vehicle的Run
8、函数调用endl;virtual void Stop()cout基类Vehicle的Stop函数调用endl;class bicycle :virtual public Vehicle public: 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函数调用Run(); p-Stop();int main() Vehicle V; bicycle B; motorcar M; motorcycle Mc; fun(&V); fun(&B);
温馨提示
- 1. 本站所有资源如无特殊说明,都需要本地电脑安装OFFICE2007和PDF阅读器。图纸软件为CAD,CAXA,PROE,UG,SolidWorks等.压缩文件请下载最新的WinRAR软件解压。
- 2. 本站的文档不包含任何第三方提供的附件图纸等,如果需要附件,请联系上传者。文件的所有权益归上传用户所有。
- 3. 本站RAR压缩包中若带图纸,网页内容里面会有图纸预览,若没有图纸预览就没有图纸。
- 4. 未经权益所有人同意不得将文件中的内容挪作商业或盈利用途。
- 5. 人人文库网仅提供信息存储空间,仅对用户上传内容的表现方式做保护处理,对用户上传分享的文档内容本身不做任何修改或编辑,并不能对任何下载内容负责。
- 6. 下载文件中如有侵权或不适当内容,请与我们联系,我们立即纠正。
- 7. 本站不保证下载资源的准确性、安全性和完整性, 同时也不承担用户因使用这些下载资源对自己和他人造成任何形式的伤害或损失。
最新文档
- 2025年度按揭车抵押借款合同资产评估与定价
- 2025年度玻璃幕墙设计与施工一体化服务合同
- 2025年度地暖系统研发与生产质量标准合同
- 2025年保鲜库自动化控制系统集成合同
- 2025年度环保污水处理承包加工合同
- 2025年度老旧小区改造租赁合同范本
- 班级学习方法的互相借鉴计划
- 供应链优化的职业发展计划
- 美术教育研究的热点与发展趋势计划
- 幼儿园小班的突发事件应急预案工作计划
- 2023年开工第一课及复工复产考试试题(含答案)
- 华为认证HCIA-Security安全H12-711考试题库及答案
- 建筑工地春节前安全教育
- (正式版)YST 1682-2024 镁冶炼行业绿色工厂评价要求
- DL-T 5148-2021水工建筑物水泥灌浆施工技术条件-PDF解密
- 宗教知识的课件
- GB/T 22849-2024针织T恤衫
- JGJ6-2011 高层建筑筏形与箱形基础技术规范
- (详尽多应用版)钢结构工程合同范本(完整版)
- 设备维保的维修流程与服务流程
- 隔膜计量泵维护检修规程培训
评论
0/150
提交评论