桂 林 理 工 大 学
实 验 报 告
实验名称 多态性 日期 2019年 05 月15 日
一、实验目的:
1、 掌握运算符重载的方法。、
2、学习使用虚函数实现动态多态性。
二、实验环境:
Visual C++
三、实验内容:
(写出主要的内容)
1. 定义Point类,有坐标x,y两个成员变量;对Point类重载+、-、++(左)、++(右)四种运算符,实现对坐标值的运行与改变。
#include<iostream>
using namespace std;
class Point
{
private:
int x,y;
public:
Point(int xx=0,int yy=0);
Point operator+(Point p2);
Point operator-(Point P2);
Point operator++();//左加加运算
Point operator++(int);
int Getx();
int Gety();
};
Point::Point(int xx,int yy)
{
x=xx;
y=yy;
}
Point Point::operator+(Point p2)
{
Point p;
p.x=x+p2.x;
p.y=y+p2.y;
return p;
}
Point Point::operator-(Point p2)
{
return Point(x-p2.x,y-p2.y );
}
Point Point::operator++()
{
x++;
y++;
cout<<"左加加被执行!"<<endl;
return *this;
}
int Point::Getx()
{
return x;
}
int Point::Gety()
{
return y;
}
Point Point::operator++(int) //重载++(右),增加一个伪参数,以便与++(左)区别
{ Point temp; //用临时变量存放当前对象原来的值,这三条语句(Point temp; temp.x=x; temp.y=y;)也可以用这个语句替代:Point temp(*this);
temp.x=x;
temp.y=y;
x++;
y++;
cout<<"右++被执行"<<endl;
return temp;
}
int main()
{
Point A,B(3,4),C,D,PA,PB;
cout<<"A原来的值:"<<A.Getx()<<","<<A.Gety()<<endl;
PA=++A;
cout<<"A的值为:"<<A.Getx()<<","<<A.Gety()<<endl;
cout<<"PA的值为:"<<PA.Getx()<<","<<PA.Gety()<<endl;
cout<<"B原来的值:"<<B.Getx()<<","<<B.Gety()<<endl;
PB=++B;
cout<<"B的值为:"<<B.Getx()<<","<<B.Gety()<<endl;
cout<<"PB的值为:"<<PB.Getx()<<","<<PB.Gety()<<endl;
PB=B++;
cout<<"B的值为:"<<B.Getx()<<","<<B.Gety()<<endl;
cout<<"PB的值为:"<<PB.Getx()<<","<<PB.Gety()<<endl;
C=A+B;
cout<<"C的值:"<<C.Getx()<<","<<C.Gety()<<endl;
D=A-B;
cout<<"D的值:"<<D.Getx()<<","<<D.Gety()<<endl;
}
输出结果:
A原来的值:0,0
左加加被执行!
A的值为:1,1
PA的值为:1,1
B原来的值:3,4
左加加被执行!
B的值为:4,5
PB的值为:4,5
右++被执行
B的值为:5,6
PB的值为:4,5
C的值:6,7
D的值:-4,-5
2.修改程序lab7_2.cpp,给bicycle、motorcar、motorcycle这三个类均定义Run、Stop等成员函数,而main函数不变,编译运行,观察结果并作分析。
3.修改上面的main函数,定义一个基类的指针,再用这个指针分别调用这几个类的对象的成员函数,编译运行,观察结果并分析原因;把基类的Run、Stop定义为虚函数,再编译运行,观察结果并分析原因;把其他类的Run、Stop也定义为虚函数,看看是否有变化。
#include<iostream>
using namespace std;
class Vehicle
{
private:
int MaxSpeed;
int Weigth;
public:
Vehicle();
~Vehicle();
void Run();
void Stop();
};
Vehicle::Vehicle()
{
MaxSpeed=0;
Weigth=0;
cout<<"构造了一个Vehicle"<<endl;
}
Vehicle::~Vehicle()
{
cout<<"析构了一个Vehicle"<<endl;
}
void Vehicle::Run()
{
cout<<"Vehicle开始运行!"<<endl;
}
void Vehicle::Stop()
{
cout<<"Vehicle停止运行!"<<endl;
}
class bicycle:virtual public Vehicle
{
private:
int Height;
public:
bicycle();
~bicycle();
};
bicycle::bicycle()
{
cout<<"构造了一个bicycle"<<endl;
}
bicycle::~bicycle()
{
cout<<"析构了一个bicycle"<<endl;
}
class motocar:virtual public Vehicle
{
private:
int SetNumber;
public:
motocar();
~motocar();
};
motocar::motocar()
{
cout<<"构造了一个motocar"<<endl;
}
motocar::~motocar()
{
cout<<"析构了一个motocar"<<endl;
}
class motorcycle:public bicycle,public motocar
{
public:
motorcycle();
~motorcycle();
};
motorcycle::motorcycle()
{
cout<<"构造了一个motorcycle"<<endl;
}
motorcycle::~motorcycle()
{
cout<<"析构了一个motorcycle"<<endl;
}
int main()
{
Vehicle *pt;
Vehicle a;
bicycle b;
motocar c;
motorcycle d;
a.Run() ;
a.Stop() ;
b.Run() ;
b.Stop() ;
c.Run() ;
c.Stop() ;
d.Run() ;
d.Stop() ;
pt=&a;
pt->Run();
pt=&b;
pt->Run();
pt=&c;
pt->Run();
pt=&d;
pt->Run();
}
输出结果:
构造了一个motocar
构造了一个Vehicle
构造了一个bicycle
构造了一个motocar
构造了一个motorcycle
Vehicle开始运行!
Vehicle停止运行!
Vehicle开始运行!
Vehicle停止运行!
Vehicle开始运行!
Vehicle停止运行!
Vehicle开始运行!
Vehicle停止运行!
Vehicle开始运行!
Vehicle开始运行!
Vehicle开始运行!
Vehicle开始运行!
析构了一个motorcycle
析构了一个motocar
析构了一个bicycle
析构了一个Vehicle
析构了一个motocar
析构了一个Vehicle
析构了一个bicycle
析构了一个Vehicle
析构了一个Vehicle
四、 心得体会:
1、学会掌握如何使用运算符重载的方法。
2、学习并且能够使用虚函数实现动态多态性。