当前位置:文档之家› 东北大学c++实验报告

东北大学c++实验报告

实验六
1•实验要求
(1)定义Point类,有坐标_x, _y两个成员变量;对Point类重载牛+ ”(自增)、“一-(自减)运算符,实现对坐标值的改变。

(2)定义一个车(vehiele)基类,有Run、Stop等成员函数,由此派生出自行车(bicycle)类、汽车(motorcar)类,从bicycle和motorcar派生出摩托车(motorcycle)类,它们都有Run、Stop等成员函数。

观察虚函数的作用。

2.实验容及实验步骤
(1)编写程序定义Point类,在类中定义整型的私有成员变量_x_y,定义成员函数Point& operato叶+() ; Point operato叶+(int);以实现对Point 类重载++ ”(自增)运算符,定义成员函数Point& operator ------------------------------ ();Point operator -------- (int);以实现对Point类重载(自减)运算符,实现对坐标值的改变。

程序名:1ab8_1. cpp。

⑵编写程序定义一个车(vehicle)基类,有Run、Stop等成员函数,由此派
生出自行车(bicycle)类、汽车(motorcar)类,从bicycle和motorcar派生出摩托车(motorcycle)类,它们都有Run、Stop等成员函数。

在main()函数中定义vehicle、bicycle、motorcar、motorcycle 的对象,调用其Run()、Stop()函数,观察其执行情况。

再分别用vehicle类型的指针来调用这几个对象的成员函数,看看能否成功;把Run、Stop定义为虚函数,再试试看。

程序名:lab8_2. cpp。

3.源程序
Lab8 1
#in clude<iostream> using n amespace std;
class Poi nt{
public:
Poi nt(i nt X,i nt Y): _x(X),_y(Y){}
Point operator++();
Point operator++(i nt);
Point operator--。


Point operator--。

nt);
void Putout() con st;
private:
int _x,_y;
};
void Poin t::Putout() con st{ coutvv"("vv_xvv","vv_yvv")"v<e ndl;
}
Point Poin t:: operator++(){
_x++;
_y++;
return *this;
Point Poin t::operator++(i nt){
++_x;++_y;
return *this;
}
Point Poin t::operator--(){
_x--;
_y--;
return *this;
}
Point Poi nt::operator--(i nt){
--_x;
--_y;
return *this;
}
int mai n(){
Poi nt A(6,7); cout<<" "Poi nt++:
(A++).Putout(); cout<<"++Poi nt:
(++A).Putout();
cout<<"Poi nt--:"
(A--).Putout(); cout«"--Poi nt:" (--A).Putout(); return 0; }
class bicycle :public Vehicle
{ 运行结果
Lab8_2
1) 在 main()函数中定义 vehicle 、bicycle 、motorcar 、motorcycle 的对象,调用 其 Run()、
Stop()函数。

#in clude<iostream> using n amespace std;
class Vehicle{
public:
void Run(){cout<<"基类 Vehicle 的 Run 函数调用"<<endl;}
void Stop(){cout<<"基类 Vehicle 的 Stop 函数调用
"<<endl;} };
public:
void Run(){cout<<"派生类bicycle 的Run 函数调用"<<endl;}
void Stop(){cout<<"派生类bicycle 的Stop 函数调用"<<endl;} };
class motorcar :public Vehicle
{
public:
void Run(){cout<<"派生类motorcar 的Run 函数调用"<<endl;} void Stop(){cout<<"派生类motorcar 的Stop 函数调用"<<endl;} };
class motorcycle :public bicycle,public motorcar
{
public:
void Run(){cout<<"派生类motorcycle 的Run 函数调用"<<endl;} void Stop(){cout<<"派生类motorcycle 的Stop 函数调用"<<endl;} };
int mai n(){
Vehicle V;
bicycle B;
motorcar M;
V.Ru n(); V.Stop();
motorcycle Mc;
class bicycle :virtual public Vehicle
{ B.R un (); B.Stop();
M.Ru n(); M.Stop();
Mc.R un(); Mc.Stop();
return 0;
}
运行结果
2)用vehicle 类型的指针来调用这几个对象的成员函数
#in clude<iostream> using n amespace std;
class Vehicle{
public:
virtual void Run(){cout<<"基类 Vehicle 的 Run 函数调用"<<endl;}
virtual void Stop(){cout<<"基类 Vehicle 的 Stop 函数调用
"<<endl;} };
public:
void Run(){cout<<"派生类bicycle 的Run 函数调用"<<endl;}
void Stop(){cout<<"派生类bicycle 的Stop 函数调用"<<endl;} };
class motorcar :virtual public Vehicle
{
public:
void Run(){cout<<"派生类motorcar 的Run 函数调用"<<endl;} void Stop(){cout<<"派生类motorcar 的Stop 函数调用"<<endl;} };
class motorcycle :public bicycle,public motorcar
{
public:
void Run(){cout<<"派生类motorcycle 的Run 函数调用"<<endl;} void Stop(){cout<<"派生类motorcycle 的Stop 函数调用"<<endl;} };
void fun( Vehicle *p){
p->Ru n();
p->Stop();
}
int mai n(){
Vehicle V;
bicycle B;
motorcar M; motorcycle Me; fun(&V);
fun(&B);
fun(&M);
fun(&Me);
return 0;
}
运行结果
4•思考题
(1).函数类型operator运算符(形参表)
{
函数体;
}
(2). friend函数类型operator运算符(形参表)
函数体;
}
⑶通过指向基类类型的指针和引用来调用对象的虚函数来实现。

相关主题