1、实验报告第8章 多态性报告书名称实验8-1 多态性1姓名陈高雪指导教师乐仁昌学号120352010054日期任务清单l 场景声明Point类,有坐标_x,_y两个成员变量;对Point类重载“+”(自增)、“”(自减)运算符,实现对坐标值的改变。l 实验方法编写程序声明Point类,在类中声明整型的私有成员变量_x、_y,声明成员函数Point &operator +();Point operator +(int);以实现对Point类重载“+”(自增)运算符,声明成员函数Point &operator -();Point operator -(int);以实现对Point类重载“”(自减)运
2、算符,实现对坐标值的改变。程序名:lab8_1.cppl 实验预估时间40分钟l 实验结果l #includeusing namespace std;class Pointpublic: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 -()_x-;_y-;return *this;Point Point:operator -(int)Point old=*this;-(*this);return old;void main()Point point(15,20);cout初始坐标: ;point.disypaly();coutpoint+坐标: ;(poi
4、nt+).disypaly();cout+point坐标: ;(+point).disypaly();coutPoint-坐标: ;(point-).disypaly();cout-Point坐标: ;(-point).disypaly();报告书名称实验8-2多态性2姓名指导教师学号日期任务清单l 场景声明一个车(vehicle)基类,具有Run、Stop等成员函数,由此派生出自行车(bicycle)类、汽车(motorcar)类,从bicycle和motorcar派生出摩托车(motorcycle)类,它们都有Run、Stop等成员函数。观察虚函数的作用。l 实验方法编写程序声明一个车(v
5、ehicle)基类,具有Run、Stop等成员函数,由此派生出自行车(bicycle)类、汽车(motorcar)类,从bicycle和motorcar派生出摩托车(motorcycle)类,它们都有Run、Stop等成员函数。在main()函数中声明vehicle、bicycle、motorcar、motorcycle的对象,调用其Run()、Stop()函数,观察其执行情况。再分别用vehicle类型的指针来调用这几个对象的成员函数,看看能否成功;把Run()、Stop()声明为虚函数,再试试看。程序名:lab8_2.cppl 实验预估时间35分钟l 实验结果l #includeusing
6、 namespace std;class vehiclepublic:virtualvoid Run() cout vehicle Run:display() endl; virtualvoid 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:vi
7、rtual 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 Run:display() endl; void Stop() cout motorcycle Stop:display() endlRun();prt-Stop();void main(
8、)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);l #includeusing namespace std;class vehiclepublic:virtualvoid Run() cout vehicle Run:display() endl; virtualvoid Stop() cout vehicle Stop:di
9、splay() endlendl; ;class bicycle: virtual public vehiclepublic:void Run() cout bicycle 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; ;c
10、lass motorcycle: public bicycle,public motorcarpublic:void Run() cout motorcycle Run:display() endl; void Stop() cout motorcycle Stop:display() endlendl; ;void main()vehicle vehicle1;vehicle1.Run();vehicle1.Stop();bicycle bicycle1;bicycle1.Run();bicycle1.Stop();motorcar motorcar1;motorcar1.Run();motorcar1.Stop();motorcycle motorcycle1;motorcycle1.Run();motorcycle1.Stop();