实验四多态性Word格式.docx
- 文档编号:17246667
- 上传时间:2022-11-29
- 格式:DOCX
- 页数:19
- 大小:44.22KB
实验四多态性Word格式.docx
《实验四多态性Word格式.docx》由会员分享,可在线阅读,更多相关《实验四多态性Word格式.docx(19页珍藏版)》请在冰豆网上搜索。
cout<
<
this->
real<
"
+i"
imag<
endl;
private:
doublereal,imag;
};
voidmain()
Complexc1(2.5,3.7);
c1.print();
Complexc2(4.2,6.5);
c2.print();
Complexc3=c1+c2;
c3.print();
getchar();
}
2、编写一个程序,其中设计一个时间类Time,用来保存时、分、秒等私有数据成员,通过重载操作符“+”实现两个时间的相加。
要求将小时范围限制在大于等于0,分钟范围限制在0~59分,秒钟范围限制在0~59秒。
提示:
时间类Time的参考框架如下:
classTime
public:
Time(inth=0,intm=0,ints=0);
//构造函数
Timeoperator+(Time&
);
//运算符重载函数,实现两个时间的相加
voiddisptime();
//显示时间函数
private:
inthours,minutes,seconds;
代码:
iostream.h>
Time(inth=0,intm=0,ints=0)//构造函数
hours=h;
minutes=m;
seconds=s;
};
a)//运算符重载函数,实现两个时间的相加
Timet(this->
hours+a.hours,this->
minutes+a.minutes,this->
seconds+a.seconds);
if(t.seconds>
59)
{
t.seconds-=60;
t.minutes+=1;
}
if(t.minutes>
t.minutes-=60;
t.hours+=1;
returnt;
voiddisptime()//显示时间函数
hours<
小时"
minutes<
分钟"
seconds<
秒"
Timet1(1,2,3);
t1.disptime();
Timet2(4,5,6);
t2.disptime();
(t1+t2).disptime();
3、用友元运算符函数或成员运算符函数,重载运算符“+”、“-”、“*”,实现对实验二中实现的矩阵类的对象的加、减、乘法。
usingnamespacestd;
classMatrix
introw,col;
int*m;
int&
operator()(inti,intj)
returnm[(i-1)*this->
col+j-1];
Matrixoperator-(Matrix&
a)
if(a.row!
=this->
row||a.col!
col)
cout<
减?
不?
了¢
?
returna;
Matrixresult(row,col);
for(inti=1;
i<
=row;
i++)
for(intj=1;
j<
=col;
j++)
{
result(i,j)=this->
m[(i-1)*col+j-1]-a(i,j);
}
returnresult;
Matrixoperator*(Matrix&
乘?
Matrixresult(a.row,col);
inttemp=0;
for(intk=1;
k<
col;
k++)
temp=this->
m[(i-1)*col+k-1]*a(k,j);
Matrixoperator+(Matrix&
加¨
®
m[(i-1)*col+j-1]+a(i,j);
Matrix(intr,intc)
inta=r*c;
m=newint[a];
row=r;
col=c;
{
for(intj=1;
{
cout<
m[(i-1)*col+j-1]<
"
;
4、编写一个程序,用于进行集合的和、并和交运算。
例如输入整数集合{9,5,4,3,6,7}和{2,4,6,9},计算出他们进行集合的并、差和交运算后的结果。
【提示】
(1)可以用一下表达式实现整数集合的基本运算:
s1+s2两个整数集合的并运算
s1-s2两个整数集合的差运算
s1*s2两个整数集合的交运算
(2)参考以下Set类的框架,用于完成集合基本运算所需的各项功能。
classSet
Set();
voidinput(intd);
//向集合中添加一个元素
intlength();
//返回集合中的元素个数
intgetd(inti);
//返回集合中位置i的元素
voiddisplay();
//显示集合的所有元素
Setoperator+(Sets1);
//成员运算符重载函数,实现集合的并运算
Setoperator-(Sets1);
//成员运算符重载函数,实现集合的差运算
Setoperator*(Sets1);
//成员运算符重载函数,实现集合的交运算
Setoperator=(Sets1);
//成员运算符重载函数,实现集合的赋值运算
protected:
intlen;
//统计结合中元素的个数;
ints[MAX];
//存放集合中的元素
Set(intl=0)
len=l;
voidinput(intd)
s[len]=d;
len++;
intlength()
returnlen;
intgetd(inti)
returns[i-1];
voiddisplay()
for(inti=0;
len;
s[i]<
Setoperator+(Sets1)
Setres(0);
intflag=1;
for(inti=0;
res.input(s[i]);
for(intj=0;
s1.len;
flag=1;
for(i=0;
if(s1.s[j]==s[i])
flag=0;
if(flag)
res.input(s1.s[j]);
returnres;
Setoperator-(Sets1)
Setres();
intflag=0;
for(inti=0;
input(s1.s[j]);
if(s1.s[i]==s[j])
input(s[j]);
Setoperator*(Sets1)
intflag=0;
flag=1;
Setoperator=(Sets1){
len=0;
for(inti=0;
input(s1.getd(i));
ints[20];
voidmain()
Seta;
a.operator=(Sets1);
a.operator*(Sets1);
a.operator+(Sets1);
a.operator-(Sets1);
5、下面提供一个体会继承中的多态性和虚函数在多态性中的作用的题目。
请根据提示进行实验。
定义类BaseFly,其中Fly()函数输出特定内容。
例如:
classBaseFly
public:
voidFly()(cout<
\n-----CIassBaseFly:
:
Fly()-----\n"
;
};
(1)定义类BirdFly、DragonFly和PlaneFly,都继承自BaseFly,重载Fly()函数,使得各类中的Fly()函数分别输出不同的内容。
classBirdFly:
publicBaseFly
voidFly(){cout<
\n-----classBirdFly:
classDragonFly:
voldFly(){cout<
\n-----ClassDragonFly:
)
);
classPlaneFly:
voidFly(){cout<
\n-----ClassPlaneFly:
(2)在main()函数中,用“new”关键字分配出以上四个类的实例,调用各个实例的Fly()函数测试多态性。
请参考以下代码:
intmain()
cout<
\n测试继承中的多态性(Virtual?
Ornot?
):
\n'
BaseFly*pBase;
BirdFly*pBird=newBirdFly;
pBasc=pBird;
\nBirdFly->
\n"
pBase->
Fly();
DragonFly*pDragon=newDragonFly;
pBase=pDragon;
\nDragonFly->
PlaneFly*pPlane=newPlaneFly;
pBase=pPlane;
\nPlaneFly->
Fly();
return0,
(3)将BaseFly:
Fly()声明为virtual,在main()中定义BaseFly的指针:
*pBase,依次分别指向UirdFly、DragonFly和P1aneFly,并调用各类的Fly()函数,体会虚函数作用。
BaseFly*pBase=newBaseFly;
pBase=pBird;
程序代码:
classBaseFly
voidFly(){cout<
BaseFly*pBase;
BirdFly*pBird=newBirdFly;
pBase=pBird;
DragonFly*pDragon=newDragonFly;
pBase=pDragon;
pBase=pPlane;
BaseFly*pBase=newBaseFly;
return0;
6、写一个程序,定义抽象类Container:
classContainer
doubleradius;
Container(doubler);
//抽象类Container的构造函数
virtualdoublesurface_area()=0;
//纯虚函数surface_area
virtualdoublevolume()=0;
//纯虚函数volume
【要求】
建立3个继承Container的派生类:
Sphere(球体)、Cylinder(圆柱体)、Cube(正方体),让每一个派生类都包含虚函数surface_area()和volume(),分别用来球体、圆柱体和正方体的表面积和体积。
要求写出主程序,应用C++的多态性,分别计算边长为6.0的正方体、半径为5.0的球体,以及半径为5.0和高为6.0的圆柱体的表面积和体积。
Container()
Container(doubler)//抽象类Container的构造函数
radius=r;
classSphere:
publicContainer
Sphere(doubler)
doublesurface_area()
4*radius*radius*3.14<
return0;
doublevolume()
4/3*radius*radius*radius*3.14<
classCylinder:
doubleheight;
Cylinder(doubler,doubleh)
height=h;
(2*radius*radius*3.14)+(2*radius*3.14*height)<
height*radius*radius*3.14<
classCube:
Cube(doubler)
radius*radius*6<
radius*radius*radius<
Cubea(6.0);
a.surface_area();
a.volume();
Sphereb(5.0);
b.surface_area();
b.volume();
Cylinderc(5.0,6.0);
c.surface_area();
c.volume();
- 配套讲稿:
如PPT文件的首页显示word图标,表示该PPT已包含配套word讲稿。双击word图标可打开word文档。
- 特殊限制:
部分文档作品中含有的国旗、国徽等图片,仅作为作品整体效果示例展示,禁止商用。设计者仅对作品中独创性部分享有著作权。
- 关 键 词:
- 实验 多态性