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

上传人:b****7 文档编号:15412456 上传时间:2023-07-04 格式:DOCX 页数:16 大小:114.66KB
下载 相关 举报
C++上机实验报告实验六.docx_第1页
第1页 / 共16页
C++上机实验报告实验六.docx_第2页
第2页 / 共16页
C++上机实验报告实验六.docx_第3页
第3页 / 共16页
C++上机实验报告实验六.docx_第4页
第4页 / 共16页
C++上机实验报告实验六.docx_第5页
第5页 / 共16页
C++上机实验报告实验六.docx_第6页
第6页 / 共16页
C++上机实验报告实验六.docx_第7页
第7页 / 共16页
C++上机实验报告实验六.docx_第8页
第8页 / 共16页
C++上机实验报告实验六.docx_第9页
第9页 / 共16页
C++上机实验报告实验六.docx_第10页
第10页 / 共16页
C++上机实验报告实验六.docx_第11页
第11页 / 共16页
C++上机实验报告实验六.docx_第12页
第12页 / 共16页
C++上机实验报告实验六.docx_第13页
第13页 / 共16页
C++上机实验报告实验六.docx_第14页
第14页 / 共16页
C++上机实验报告实验六.docx_第15页
第15页 / 共16页
C++上机实验报告实验六.docx_第16页
第16页 / 共16页
亲,该文档总共16页,全部预览完了,如果喜欢就下载吧!
下载资源
资源描述

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

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

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

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

实验六多态性

1.实验目的

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

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

2.实验要求

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

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

观察虚函数的作用。

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

3.实验内容及实验步骤

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

程序名:

1ab8_1.cpp。

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

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

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

程序名:

lab8_2.cpp。

4.思考题

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

函数类型operator运算符(形参表)

{

函数体;

}

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

friend函数类型operator运算符(形参表)

{

函数体;

}

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

在基类的成员函数前加上virtual,就可以在它的派生类中声明相同名字和类型的成员函数,在运行过程中,系统会自动判断并调用相应类中的成员函数,从而在调用过程中实现多态。

5.源程序

1.lab8_1.cpp

#include

usingnamespacestd;

classPoint

{

private:

int_x;

int_y;

public:

//构造.析构函数

Point(){}

Point(int,int);

~Point(){}

//++.--重载

Point&operator++();

Pointoperator++(int);

Point&operator--();

Pointoperator--(int);

//输出点坐标

voidshowPoint();

};

Point:

:

Point(intx,inty)

{

_x=x;

_y=y;

}

Point&Point:

:

operator++()

{

_x++;

_y++;

return*this;

}

PointPoint:

:

operator++(int)

{

Pointp=*this;

++(*this);

returnp;

}

Point&Point:

:

operator--()

{

_x--;

_y--;

return*this;

}

PointPoint:

:

operator--(int)

{

Pointp=*this;

--(*this);

returnp;

}

voidPoint:

:

showPoint()

{

cout<<"Thepointis("<<_x<<","<<_y<<")"<

}

intmain()

{

Pointapoint(3,5);

apoint.showPoint();

(apoint++).showPoint();//测试后置++

apoint.showPoint();

(++apoint).showPoint();//测试前置++

apoint.showPoint();

(apoint--).showPoint();//测试后置--

apoint.showPoint();

(--apoint).showPoint();//测试前置--

apoint.showPoint();

return0;

}

2.lab8_2.cpp

#include

usingnamespacestd;

classVehicle

{

public:

//基类的成员函数为虚函数

virtualvoidrun(){cout<<"Vehicleisrunning!

"<

virtualvoidstop(){cout<<"Vehicleisstopping!

"<

};

classBicycle:

virtualpublicVehicle//按虚基类继承

{

public:

voidrun(){cout<<"Bicycleisrunning!

"<

voidstop(){cout<<"Bicycleisstopping!

"<

};

classMotorcar:

virtualpublicVehicle//按虚基类继承

{

public:

voidrun(){cout<<"Motorcarisrunning!

"<

voidstop(){cout<<"Motorcarisstopping!

"<

};

classMotorcycle:

publicBicycle,publicMotorcar

{

public:

voidrun(){cout<<"Motorcycleisrunning!

"<

voidstop(){cout<<"Motorcycleisstopping!

"<

};

intmain()

{

Vehicleveh;

Bicyclebic;

Motorcarmot;

Motorcyclem;

//对象名.函数测试

/*veh.run();

veh.stop();

bic.run();

bic.stop();

mot.run();

mot.stop();

m.run();

m.stop();*/

//基类指针测试

Vehicle*p;

p=&veh;

p->run();

p->stop();

p=&bic;

p->run();

p->stop();

p=&mot;

p->run();

p->stop();

p=&m;

p->run();

p->stop();

return0;

}

3.lab8_3

#include

#include

usingnamespacestd;

//Date类

classDate

{

private:

intyear;

intmonth;

intday;

public:

Date();

Date(inty,intm,intd);

Date(Date&p);

~Date();

voidsetDate();

voidshowDate();

};

//People类,其中含Date类型的数据

classPeople

{

private:

charname[11];

charnumber[7];

charsex[3];

Datebirthday;

public:

charid[16];

People();

People(char*n,char*nu,char*s,Dateb,char*i);

People(People&p);

~People();

booloperator==(People&);

People&operator=(People&);

voidsetName();

voidsetNumber();

voidsetSex();

voidsetId();

voidshowPeople();

};

//Date构造函数

Date:

:

Date(){}

Date:

:

Date(inty,intm,intd)

{

year=y;

month=m;

day=d;

}

Date:

:

Date(Date&p)

{

year=p.year;

month=p.month;

day=p.day;

}

//析构

inlineDate:

:

~Date(){}

//Date成员函数,设置出生年月日

voidDate:

:

setDate()

{

inty,m,d;

cout<<"Inputtheyear:

";

cin>>y;

cout<<"Inputthemonth:

";

cin>>m;

cout<<"Inputtheday:

";

cin>>d;

year=y;

month=m;

day=d;

}

//Date内联成员函数,输出年月日

inlinevoidDate:

:

showDate()

{

cout<<"Birthdayis"<

}

//People构造函数

People:

:

People(){};

People:

:

People(char*n,char*nu,char*s,Dateb,char*i)

{

strcpy(name,n);

strcpy(number,nu);

strcpy(sex,s);

birthday=b;

strcpy(id,i);

}

People:

:

People(People&p)

{

strcpy(name,p.name);

strcpy(number,p.number);

birthday=p.birthday;

strcpy(id,p.id);

}

//People析构

inlinePeople:

:

~People(){}

//People成员函数,设置各类数据

voidPeople:

:

setName()

{

cout<<"Pleaseinputtheperson'sname:

";

cin.getline(name,11,'\n');

}

voidPeople:

:

setNumber()

{

cout<<"Inputnumber:

";

cin.getline(number,7,'\n');

}

voidPeople:

:

setSex()

{

cout<<"Inputsex:

";

cin.getline(sex,3,'\n');

}

voidPeople:

:

setId()

{

cout<<"Inputid:

";

cin.getline(id,16,'\n');

}

//People内联成员函数,输出人员信息

inlinevoidPeople:

:

showPeople()

{

cout<<"Name:

"<

cout<<"Number:

"<

cout<<"Sex:

"<

cout<<"ID:

"<

}

boolPeople:

:

operator==(People&p)

{

returnid==p.id;

}

People&People:

:

operator=(People&p)

{

strcpy(name,p.name);

strcpy(number,p.number);

birthday=p.birthday;

strcpy(id,p.id);

return*this;

}

//检测==重载的普通函数

voidtest(People&x,People&y)

{

if(strcmp(x.id,y.id)==0)

{

cout<<"Their'sIDsaresame"<

}

else

{

cout<<"Their'sIDsaredifferent"<

}

}

intmain()

{

/*inti;

charspaceA;

//生成3个Date类型的对象

Datedate[3]={Date(0,0,0),Date(0,0,0),Date(0,0,0)};

//生成3个People类型的对象

Peopleperson[3]={People("0","0","0",date[0],"0"),People("0","0","0",date[1],"0"),People("0","0","0",date[2],"0")};

//设置这3个对象的各类信息

for(i=0;i<3;i++)

{

person[i].setName();

person[i].setNumber();

person[i].setSex();

person[i].setId();

date[i].setDate();

spaceA=getchar();

}

//输出这3个对象的各类信息

for(i=0;i<3;i++)

{

person[i].showPeople();

date[i].showDate();

}*/

//测试==重载

Dated1(0,0,0);

Peoplep1("lizhibo","01","male",d1,"123456");

Peoplep2("wangyusen","02","male",d1,"123");

Peoplep3("zhouhao","03","male",d1,"123456");

test(p1,p2);

test(p1,p3);

//测试=重载

cout<<"Before="<

p1.showPeople();

p1=p3;

cout<<"After="<

p1.showPeople();

return0;

}

6.运行结果

1.

2.

直接使用对象.函数的形式可以成功调用函数:

使用基类指针后出现错误,只能调用基类的成员函数:

将基类的成员函数设置成虚函数之后,成功实现调取各个派生类的成员函数:

其中在使用Vehicle类型指针指向Motorcycle类型的对象时会出现错误:

将Vehicle按照虚基类继承,问题解决。

3.

7.心得体会

学习了解了多态,通过编写程序,进行上机练习,学会了如何利用虚函数实现程序的多态性,进行函数的重载;而且学会了如何对各类运算符进行重载,使得各类运算符满足同类之间的运算,使得程序更加高效;还学会了使用基类指针或引用对基类的派生类中的各类重载函数进行调用,收获很大。

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

当前位置:首页 > 表格模板 > 合同协议

copyright@ 2008-2023 冰点文库 网站版权所有

经营许可证编号:鄂ICP备19020893号-2