C++实验报告8.docx
- 文档编号:20648062
- 上传时间:2023-04-25
- 格式:DOCX
- 页数:10
- 大小:77.66KB
C++实验报告8.docx
《C++实验报告8.docx》由会员分享,可在线阅读,更多相关《C++实验报告8.docx(10页珍藏版)》请在冰豆网上搜索。
C++实验报告8
实验报告
第8章多态性
报告书
名称
实验8-1多态性1
姓名
陈高雪
指导教师
乐仁昌
学号
日期
任务清单
场景
声明Point类,有坐标_x,_y两个成员变量;对Point类重载“++”(自增)、“——”(自减)运算符,实现对坐标值的改变。
实验方法
编写程序声明Point类,在类中声明整型的私有成员变量_x、_y,声明成员函数Point&operator++();Pointoperator++(int);以实现对Point类重载“++”(自增)运算符,声明成员函数Point&operator--();Pointoperator--(int);以实现对Point类重载“——”(自减)运算符,实现对坐标值的改变。
程序名:
实验预估时间
40分钟
实验结果
#include
usingnamespacestd;
classPoint
{
public:
Point(intx=0,inty=0);
Point&operator++();
Pointoperator++(int);
Point&operator--();
Pointoperator--(int);
voiddisypaly();
private:
int_x,_y;
};
Point:
:
Point(intx,inty)
{
_x=x;
_y=y;
}
voidPoint:
:
disypaly()
{
cout<<"("<<_x<<","<<_y<<")"< } Point&Point: : operator++() { _x++; _y++; return*this; } PointPoint: : operator++(int) { Pointold=*this; ++(*this); returnold; } Point&Point: : operator--() { _x--; _y--; return*this; } PointPoint: : operator--(int) { Pointold=*this; --(*this); returnold; } voidmain() { Pointpoint(15,20); cout<<"初始坐标: "; (); cout<<"point++坐标: "; (point++).disypaly(); cout<<"++point坐标: "; (++point).disypaly(); cout<<"Point--坐标: "; (point--).disypaly(); cout<<"--Point坐标: "; (--point).disypaly(); } 报告书 名称 实验8-2多态性2 姓名 指导教师 学号 日期 任务清单 场景 声明一个车(vehicle)基类,具有Run、Stop等成员函数,由此派生出自行车(bicycle)类、汽车(motorcar)类,从bicycle和motorcar派生出摩托车(motorcycle)类,它们都有Run、Stop等成员函数。 观察虚函数的作用。 实验方法 编写程序声明一个车(vehicle)基类,具有Run、Stop等成员函数,由此派生出自行车(bicycle)类、汽车(motorcar)类,从bicycle和motorcar派生出摩托车(motorcycle)类,它们都有Run、Stop等成员函数。 在main()函数中声明vehicle、bicycle、motorcar、motorcycle的对象,调用其Run()、Stop()函数,观察其执行情况。 再分别用vehicle类型的指针来调用这几个对象的成员函数,看看能否成功;把Run()、Stop()声明为虚函数,再试试看。 程序名: 实验预估时间 35分钟 实验结果 #include usingnamespacestd; classvehicle { public: virtualvoidRun() { cout<<"vehicleRun: : display()"< } virtualvoidStop() { cout<<"vehicleStop: : display()"< } }; classbicycle: virtualpublicvehicle { public: voidRun() { cout<<"bicycleRun: : display()"< } voidStop() { cout<<"bicycleStop: : display()"< } }; classmotorcar: virtualpublicvehicle { public: voidRun() { cout<<"motorcarRun: : display()"< } voidStop() { cout<<"motorcarStop: : display()"< } }; classmotorcycle: publicbicycle,publicmotorcar { public: voidRun() { cout<<"motorcycleRun: : display()"< } voidStop() { cout<<"motorcycleStop: : display()"< } }; voidfun(vehicle*prt) { prt->Run(); prt->Stop(); } voidmain() { vehiclevehicle1,*p; bicyclebicycle1; motorcarmotorcar1; motorcyclemotorcycle1; p=&vehicle1; fun(p); p=&bicycle1; fun(p); p=&motorcar1; fun(p); p=&motorcycle1; fun(p); } #include usingnamespacestd; classvehicle { public: virtualvoidRun() { cout<<"vehicleRun: : display()"< } virtualvoidStop() { cout<<"vehicleStop: : display()"< } }; classbicycle: virtualpublicvehicle { public: voidRun() { cout<<"bicycleRun: : display()"< } voidStop() { cout<<"bicycleStop: : display()"< } }; classmotorcar: virtualpublicvehicle { public: voidRun() { cout<<"motorcarRun: : display()"< } voidStop() { cout<<"motorcarStop: : display()"< } }; classmotorcycle: publicbicycle,publicmotorcar { public: voidRun() { cout<<"motorcycleRun: : display()"< } voidStop() { cout<<"motorcycleStop: : display()"< } }; voidmain() { vehiclevehicle1; (); (); bicyclebicycle1; (); (); motorcarmotorcar1; (); (); motorcyclemotorcycle1; (); (); }
- 配套讲稿:
如PPT文件的首页显示word图标,表示该PPT已包含配套word讲稿。双击word图标可打开word文档。
- 特殊限制:
部分文档作品中含有的国旗、国徽等图片,仅作为作品整体效果示例展示,禁止商用。设计者仅对作品中独创性部分享有著作权。
- 关 键 词:
- C+ 实验 报告