C++上机实验报告实验六.docx

上传人:b****6 文档编号:7149099 上传时间:2023-01-21 格式:DOCX 页数:10 大小:17.04KB
下载 相关 举报
C++上机实验报告实验六.docx_第1页
第1页 / 共10页
C++上机实验报告实验六.docx_第2页
第2页 / 共10页
C++上机实验报告实验六.docx_第3页
第3页 / 共10页
C++上机实验报告实验六.docx_第4页
第4页 / 共10页
C++上机实验报告实验六.docx_第5页
第5页 / 共10页
点击查看更多>>
下载资源
资源描述

C++上机实验报告实验六.docx

《C++上机实验报告实验六.docx》由会员分享,可在线阅读,更多相关《C++上机实验报告实验六.docx(10页珍藏版)》请在冰豆网上搜索。

C++上机实验报告实验六.docx

C++上机实验报告实验六

集团企业公司编码:

(LL3698-KKI1269-TM2483-LUI12689-ITT289-DQS58-MG198)

 

C++上机实验报告实验六

实验六

实验目的

1.掌握运算符重载的方法

2.学习使用虚函数实现动态多态性

实验要求

1.定义Point类,有坐标_x,_y两个成员变量;对Point类重载“++”(自增)、“――”(自减)运算符,实现对坐标值的改变。

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

观察虚函数的作用。

3.(选做)对实验4中的People类重载“==”运算符和“-”运算符,“==”运算符判断两个people类对象的id属性是否相等;“-”运算符实现People类对象的赋值操作。

实验内容及实验步骤

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

程序名:

1ab8_1.Cpp

#include

usingnamespacestd;

classPoint

{

public:

Point();

Point(intx,inty);

~Point(){}//Point类析构函数

Point&operator++();//公有成员函数

Pointoperator++(int);

Point&operator--();

Pointoperator--(int);

voidShow();

private:

int_x;//私有数据成员

int_y;

};

Point:

:

Point(){//Point类构造函数

_x=0;_y=0;}

Point:

:

Point(intx,inty)//Point类构造函数

{

_x=x;

_y=y;

}

Point&Point:

:

operator++()//重载后置++运算符为Point类成员函数

{

_x++;

_y++;

}

PointPoint:

:

operator++(int)//重载前置++运算符为Point类成员函数

{

Pointold=*this;

++(this->_x);

++(this->_y);

returnold;

}

Point&Point:

:

operator--()//重载后置--运算符为Point类成员函数

{

_x--;

_y--;

}

PointPoint:

:

operator--(int)//重载前置--运算符为Point类成员函数

{

Pointold=*this;

--(this->_x);

--(this->_y);

returnold;

}

voidPoint:

:

Show()//输出Point的坐标值

{

cout<<_x<<","<<_y<<")"<

}

intmain()

{

Pointa(2,3);//定义一个Point类对象a

Pointb=a++;//定义Point类对象b并用a++初始化b

Pointc=++a;//定义Point类对象c并用++a初始化c

Pointd=--a;//定义Point类对象d并用--a初始化d

Pointe=a--;//定义Point类对象e并用a--初始化e

cout<<"Pointa(";

a.Show();//输出a的坐标

cout<<"Pointb(";

b.Show();//输出b的坐标

cout<<"Pointc(";

c.Show();//输出c的坐标

cout<<"Pointd(";

d.Show();//输出d的坐标

cout<<"Pointe(";

e.Show();//输出e的坐标

return0;

}

运行结果:

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

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

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

程序名:

lab8_2.cpp

#include

usingnamespacestd;

classvehicle//基类vehicle

{

public:

vehicle(){}

~vehicle(){}

virtualvoidRun(){cout<<"Thevehicleisrunning!

"<

virtualvoidStop(){cout<<"Thevehiclehasstopped!

"<

};

classbicycle:

virtualpublicvehicle//定义派生类bicycle,声明基类为派生类的虚基类

{

public:

voidRun(){cout<<"Thebicycleisrunning!

"<

voidStop(){cout<<"Thebicyclehasstopped!

"<

bicycle(){}

~bicycle(){}

};

classmotorcar:

virtualpublicvehicle//定义派生类motorcar,声明基类为派生类的虚基类

{

public:

voidRun(){cout<<"Themotorcarisrunning!

"<

voidStop(){cout<<"Themotorcarhasstopped!

"<

motorcar(){}

~motorcar(){}

};

classmotorcycle:

publicbicycle,publicmotorcar//以bicycle类和motorcar类作为基类派生新类motorcycle

{

public:

voidRun(){cout<<"Themotorcycleisrunning!

"<

voidStop(){cout<<"Themotorcyclehasstopped!

"<

};

intmain()

{

vehiclea;//定义vehicle类的一个对象a

vehicle*p;//定义一个vehicle类的指针

bicycleb;//定义bicycle类的对象b

motorcarc;//定义motorcar类的对象c

motorcycled;//定义motorcycle类的对象d

a.Run();

a.Stop();

b.Run();

b.Stop();

c.Run();

c.Stop();

d.Run();

d.Stop();

p=&a;//使指针p指向vehicle类对象a

p->Run();//通过指针调用vehicle类成员函数

p->Stop();

p=&b;//使指针p指向bicycle类对象b

p->Run();//通过指针调用bicycle类成员函数

p->Stop();

p=&c;//使指针p指向motorcar类对象c

p->Run();//通过指针调用motorcar类成员函数

p->Stop();

p=&d;//使指针指向motorbicycle类对象d

p->Run();//通过指针调用motorcycle类成员函数

p->Stop();

return0;

}

运行结果:

3.(选做)对实验4中的People类重载“==”运算符和“-”运算符,“==”运算符判断两个people类对象的id属性是否相等;“-”运算符实现People类对象的赋值操作。

源程序:

#include

#include

usingnamespacestd;

classBirthday{

public:

Birthday(intYear,intMonth,intDay);//构造函数

Birthday(){}//构造函数

~Birthday(){}//析构函数

Birthday(Birthday&p);//复制构造函数

intshowBirthday(){

cout<

intenter();

private:

intyear,month,day;

};

Birthday:

:

Birthday(Birthday&p){

year=p.year;

month=p.month;

day=p.day;

}

Birthday:

:

Birthday(intYear,intMonth,intDay)//Birthday类构造函数

{

year=Year;

month=Month;

day=Day;

}

intBirthday:

:

enter(){

cout<<"生日:

";

cin>>year>>month>>day;

}

classpeople{//定义people类

public:

people(){}//people类构造函数

~people(){}//people类析构函数

people(people&p);

peopleoperator==(people&);

peopleoperator-(people&);

intshow();

intenter();

private:

stringnumber,id,sex;//字符串类型变量数据成员

Birthdayp1;//Birthday类数据成员

};

intpeople:

:

show()

{

cout<<"\n性别"<

cout<

cout<<"生日";

p1.showBirthday();//调用Birthday类成员函数

cout<<"身份证号"<

}

intpeople:

:

enter(){

p1.enter();

cout<<"性别:

";

cin>>sex;

cout<<"编号:

";

cin>>number;

cout<<"身份证号:

";

cin>>id;

}

people:

:

people(people&p):

p1(p.p1)//people类复制构造函数

{

number=p.number;

sex=p.sex;

id=p.id;

}

peoplepeople:

:

operator==(people&V)//重载==运算符成员函数

{

if(id==V.id)

{

cout<<"havethesameid!

"<

}

else

{cout<<"havedifferentid!

"<

}

peoplepeople:

:

operator-(people&U)//重载-运算符成员函数

{

number=U.number;//使用字符串赋值运算符

sex=U.sex;

id=U.id;

p1=U.p1;

return*this;//返回this指针

}

intmain()

{

intt;

peoplep[2];//定义对象数组p[2]

for(t=0;t<2;t++)//输入对象数组成员信息

{

cout<<"输入第"<

p[t].enter();

}

for(t=0;t<2;t++)//输出对象数组成员信息

{

cout<<"\n第"<

"<

p[t].show();

}

peoplep3;//定义people类的对象p3

p3-p[1];//使用重载运算符将p[1]赋给p3

cout<<"\n第3个人员信息如下:

"<

p3.show();

cout<<"p[0],p[1]";

p[0]==p[1];//使用重载运算符-判断p[0]和p[1]的id是否相等

return0;

}

运行结果:

思考题

1.如何将一个运算符重载为类的成员函数?

一般语法形式:

返回类型operator运算符(形参表)

{

函数体

}

函数的参数个数比原来的曹祖数个数要少一个(后置“++”,“--”除外)。

2.如何将一个运算符重载为类的友元函数?

一般形式:

friend返回类型operator运算符(形参表)

{

函数体

}

运算所需的操作数都需要通过函数的形参表传递,在形参表中形参从左至右的顺序就是运算符操作数的顺序,仅在需要访问类的私有成员或保护成员时才定义为友元函数。

3.如何实现运行时刻的多态?

通过继承和虚函数实现。

心得体会:

掌握了运算符重载的方法;运算符重载可以重载为类成员函数和非成员函数;学会了使用继承和虚函数实现运行时刻的多态;在实现动态多态时,必须使用基类类型的指针或引用,是该指针指向基类的不同派生类的对象,并通过该指针指向虚函数才能实现动态的多态性。

如果未声明为虚函数,则普通的派生类的新成员函数会覆盖基类同名成员函数,将不能实现运行时刻多态性。

展开阅读全文
相关资源
猜你喜欢
相关搜索
资源标签

当前位置:首页 > 人文社科 > 教育学心理学

copyright@ 2008-2022 冰豆网网站版权所有

经营许可证编号:鄂ICP备2022015515号-1