一、实验目的和要求
2.学习使用虚函数实现动态多态性; |
二、实验环境(软、硬件及条件)
|
三、实验步骤
1.定义Point类,有坐标x,y两个成员变量;对Point类重载+、-、++(左)、++(右)四种运算符,实现对坐标值的运行与改变。 2.修改程序Lab.cpp,给bicycle、motorcar、motorcycle这三个类均定义Run、Stop等成员函数,观察虚函数的作用。
1.编写程序定义Point类,在类中定义整型的私有成员变量x、y,定义成员函数 Point operator + (Point p2)重载“+”运算符,定义成员函数 Point operator – (Point p2)重载“-”运算符,定义成员函数Point operator++( )重载“++(左)”运算符,定义成员函数 Point operator++(int )重载“++(右)”运算符,实现对坐标值的改变。注意观察++(左)与++(右)运算的结果是否有区别,试着重载–(左)与–(右)。 2.修改程序lab.cpp,给bicycle、motorcar、motorcycle这三个类均定义Run、Stop等成员函数,而main函数不变,编译运行,观察结果并作分析。 已知Lab.cpp如下: #include <iostream.h> class vehicle
{ private: int MaxSpeed; int Weight; public: vehicle(){MaxSpeed=0; Weight=0;}; ~vehicle(){}; void Run() {cout << “Now it is running!” << endl; } void Stop() {cout << “Now it has stopped!” << endl; } }; class bicycle : virtual public vehicle
{ private: int Height; public: bicycle(){}; ~bicycle(){}; }; class motorcar : virtual public vehicle
{ private: int SeatNum; public: motorcar(){}; ~motorcar(){}; }; class motorcycle : public bicycle , public motorcar
{ public: motorcycle (){}; ~motorcycle (){}; }; void main()
{ vehicle a; bicycle b; motorcar c; motorcycle d; a.Run(); a.Stop(); b.Run(); b.Stop(); c.Run(); c.Stop(); d.Run(); d.Stop(); } 3.修改上面的main函数,定义一个基类的指针,再用这个指针分别调用这几个类的对象的成员函数,编译运行,观察结果并分析原因;把基类的Run、Stop定义为虚函数,再编译运行,观察结果并分析原因;把其他类的Run、Stop也定义为虚函数,看看是否有变化
|
四、实验中遇到的问题及解决 无 |
五、实验结果及分析 1. #include<iostream> using namespace std; class Point {private: int x,y; public: Point(){x=0,y=0;}; Point(int m,int n){x=m,y=n;cout<<“(“<<x<<“,”<<y<<“)”<<endl;}; Point operator + (Point p2); Point operator – (Point p2); Point & operator++(); Point & operator++(int ); void show(){cout<<“(“<<x<<“,”<<y<<“)”<<endl;} }; Point Point::operator + (Point p2){Point p1(x+p2.x,y+p2.y);return p2;}; Point Point::operator – (Point p2){Point p3(x-p2.x,y-p2.y);return p2;}; Point & Point::operator++(){x++,y++;return *this;}; Point & Point::operator++(int ){++x,++y;return *this;}; void main() {Point g(3,6),f(8,9),h1=g+f,h2=g-f,m,n; g++; g.show(); g=++g; g.show(); } 2. #include <iostream.h> class vehicle
{ private: int MaxSpeed; int Weight; public: vehicle(){MaxSpeed=0; Weight=0;}; ~vehicle(){}; void Run() {cout << “Vehicle:Now it is running!” << endl; } void Stop() {cout << “Vehicle:Now it has stopped!” << endl; } }; class bicycle : virtual public vehicle
{ private: int Height; public: bicycle(){}; ~bicycle(){}; void Run() {cout << “Bicycle:Now it is running!” << endl; } void Stop() {cout << “Bicycle:Now it has stopped!” << endl; } }; class motorcar : virtual public vehicle
{ private: int SeatNum; public: motorcar(){}; ~motorcar(){}; void Run() {cout << “Motorcar:Now it is running!” << endl; } void Stop() {cout << “Motorcar:Now it has stopped!” << endl; } }; class motorcycle : public bicycle , public motorcar
{ public: motorcycle (){}; ~motorcycle (){}; void Run() {cout << “Motorcycle:Now it is running!” << endl; } void Stop() {cout << “Motorcycle:Now it has stopped!” << endl; } }; void main()
{ vehicle a; bicycle b; motorcar c; motorcycle d; a.Run(); a.Stop(); b.Run(); b.Stop(); c.Run(); c.Stop(); d.Run(); d.Stop(); }
3. 定义基类vehicle指针,只能访问bicycle和motorcar的成员函数,若定义基类bicycle或motorcar指针,只能访问motorcycle的成员函数。 基类vehicle的Run,Stop函数设为虚函数,编译结果为2.题结果;基类bicycle和motorcar的Run,Stop函数设为虚函数,编译结果如下。因为虚函数只能访问派生类从基类继承的成员。
|