付费下载
下载本文档
版权说明:本文档由用户提供并上传,收益归属内容提供方,若内容存在侵权,请进行举报或认领
文档简介
1、实验报告多态性报告书名称实验8-1多态性1姓名陈局雪指导教师乐仁昌学号120352010054日期任务清单场了严明Point类,有坐标_x, _y两个成贝变重;对 Point类重载 + (自增)、 (自减)运算符,实现对汇标值的改变。实验方法编写程序声明Point类,在类中声明整型的私有成员变量_x、_y,声明成员函数 Point&operator + () ; Point operator+ (int);以实现对 Point 类重载 + (自增)运算 符,严明成员函数Point &operator -() ; Point operator - (int);以实现对 Point 类重载“一一”
2、(自减)运算符,实现对坐标值的改变。程序名:lab8_1.cpp实验预估时间40分钟实验结果#includeusing namespace std;class Point public:Point(int x=0,int y=0);Point &operator +();Point operator +(int);Point &operator -();Point operator -(int);void disypaly();private:int _x,_y;Point:Point(int x,int y)_x=x;_y=y;void Point:disypaly()cout(_x,_y)
3、endl;Point &Point:operator +()_x+;_y+;return *this;Point Point:operator +(int)Point old=*this;+(*this);return old;Point &Point:operator -()臂品#p嘴品献疗嘴品H/p_x-;_y-;return *this;Point Point:operator -(int)Point old=*this;-(*this);return old;void main()Point point(15,20);cout初始坐标:;point.disypaly();coutpoi
4、nt+ 坐标:;(point+).disypaly();cout+point 坐标:;(+point).disypaly();coutPoint- 坐标:;(point-).disypaly();cout(15,20 to continueress anuPo int坐式C: Prof ra FilesMicrosoft Visual St.1 s s :一:e 4呻际标际k名称任务清单实验8-2多态性2指导教师日期报告书场景声明一个车 (vehicle)基类,具有Run、Stop等成员函数,由此派生出自行车(bicycle)类、汽车(motorcar)类,从 bicycle和 motorca
5、r派生出摩托车( motorcycle )类,它们都 有Run、Stop等成员函数。观察虚函数的作用。实验方法编写程序声明一个车( vehicle)基类,具有Run、Stop等成员函数,由此派生出自行车(bicycle )类、汽车(motorcar )类,从 bicycle 和 motorcar 派生出摩托车 (motorcycle ) 类,它们都有 Run、Stop等成员函数。 在main ()函数中声明 vehicle、bicycle、motorcar motorcycle的对象,调用其 Run ()、Stop ()函数,观察其执行情况。再分别用vehicle类型的指针来调用这几个对象的成
6、员函数,看看能否成功;把Run ()、Stop ()声明为虚函数,再试试看。程序名:lab8_2.cpp实验预估时间35分钟实验结果#includeusing namespace std;class vehiclepublic:virtual void Run()臂品#p嘴品献疗嘴品H/pcout vehicle Run:display() endl; virtual void Stop() cout vehicle Stop:display() endlendl; ;class bicycle: virtual public vehiclepublic:void Run() cout bic
7、ycle Run:display() endl; void Stop() cout bicycle Stop:display() endlendl; ;class motorcar:virtual public vehiclepublic:void Run() cout motorcar Run:display() endl; void Stop()一 cout motorcar Stop:display() endlendl;;class motorcycle: public bicycle,public motorcarpublic:void Run() cout motorcycle R
8、un:display() endl;void Stop()cout motorcycle Stop:display() endlRun();prt-Stop();void main()vehicle vehicle1,*p;bicycle bicycle1;motorcar motorcar1;motorcycle motorcycle1;p=&vehicle1;fun(p);p=&bicycle1;fun(p);p=&motorcar1;fun(p);科品娘川嘴品丽川嘴品丽川p=&motorcycle1;fun(p);#includeusing namespace std;class veh
9、iclepublic:virtual void Run() cout vehicle Run:display() endl;virtual void Stop() cout vehicle Stop:display() endlendl; ;class bicycle: virtual public vehiclepublic:void Run() cout bicycle Run:display() endl;void Stop() cout bicycle Stop:display() endlendl;;class motorcar:virtual public vehiclepubli
10、c:void Run() cout motorcar Run:display() endl;void Stop() cout motorcar Stop:display() endlendl; ;class motorcycle: public bicycle,public motorcarpublic:void Run()cout motorcycle Run:display() endl;臂品#p嘴品丽川嘴品H/pvoid Stop() cout motorcycle Stop:display() endlendl;; void main() vehicle vehiclel;vehicle1.Run();vehicle1.Stop();bi
温馨提示
- 1. 本站所有资源如无特殊说明,都需要本地电脑安装OFFICE2007和PDF阅读器。图纸软件为CAD,CAXA,PROE,UG,SolidWorks等.压缩文件请下载最新的WinRAR软件解压。
- 2. 本站的文档不包含任何第三方提供的附件图纸等,如果需要附件,请联系上传者。文件的所有权益归上传用户所有。
- 3. 本站RAR压缩包中若带图纸,网页内容里面会有图纸预览,若没有图纸预览就没有图纸。
- 4. 未经权益所有人同意不得将文件中的内容挪作商业或盈利用途。
- 5. 人人文库网仅提供信息存储空间,仅对用户上传内容的表现方式做保护处理,对用户上传分享的文档内容本身不做任何修改或编辑,并不能对任何下载内容负责。
- 6. 下载文件中如有侵权或不适当内容,请与我们联系,我们立即纠正。
- 7. 本站不保证下载资源的准确性、安全性和完整性, 同时也不承担用户因使用这些下载资源对自己和他人造成任何形式的伤害或损失。
最新文档
- 2026年山西省中考物理试卷附答案
- ICU危重患者的体温管理技巧
- 湖北省孝感市2026年中考化学模拟试题(含答案解析)
- 2026届吉林省松原市中考三模化学试题(含答案解析)
- 中医护理中医食疗配方解析
- 初中八年级地理《北方地区的自然特征与农业发展》导学案
- 河北技能考试试题及答案
- 八年级上册道德与法治《强军伟业:全面推进国防和军队现代化》活动探究教案
- 初三物理中考复习教案:简单机械专题精讲与突破
- 初中八年级科学教案:探索空气的组成与价值(2课时)
- 大学英语四级考试高频词汇1500(打印版)
- 能量转化与存储原理-课件全套 第1-10章 绪论- 新型能量转换与存储技术
- 2025年消防文员招聘试卷及答案
- 2025年吉林省事业单位招聘考试卫生类护理学专业知识试卷
- 技改大修工程项目管理手册与实践经验分享
- GB/T 11186-2025涂膜颜色测量方法
- 2024-2025学年山东省菏泽市高一(下)期末数学试卷(含解析)
- 国企物业薪酬管理办法
- 石料厂安全操作规程
- 幼儿园大班科学公开课《有趣的转动》课件
- 公司客户欠款管理制度
评论
0/150
提交评论