实验3多态性.docx
《实验3多态性.docx》由会员分享,可在线阅读,更多相关《实验3多态性.docx(12页珍藏版)》请在冰豆网上搜索。
实验3多态性
西华大学
实验报告
(2010/2011学年第二学期)
课程名称
面向对象程序设计
实验名称
多态性
实验时间
年
月
日
指导单位
指导教师
学生姓名
班级学号
学院(系)
专业
实验报告
实验名称
多态性实验
实验类型
综合
实验学时
2×2
一、实验目的和要求
(1)进一步熟悉类的设计、运用继承与派生机制设计派生类,合理设置数据成员和成员函数。
(2)掌握双目运算符、单目运算符的重载方法,对常用算术运算符能在自定义类中通过友元函数、成员函数进行重载,以实现静态多态性。
(3)掌握通过继承、虚函数、基类的指针或引用实现动态多态性的方法。
(4)理解并掌握有纯虚函数的抽象类的作用,在各派生类中重新定义各纯虚函数的方法,以及此时实现的动态多态性。
二、实验环境(实验设备)
硬件:
微型计算机
软件:
MicrosoftVisualC++6.0
三、实验原理及内容
实验题目
(1):
定义一个抽象类容器类Container,其中定义了若干纯虚函数,实现求表面积、体积、输出等功能。
由此抽象类派生出正方体、球体和圆柱体等多个派生类,根据需要定义自己的成员变量,在各个派生类中重新定义各纯虚函数,实现各自类中相应功能,各个类成员的初始化均由本类构造函数实现。
在主函数中,定义容器类的指针和各个派生类的对象,使指针指向不同对象处调用相同的函数能执行不同的函数代码,从而实现动态多态性。
实验解答:
1根据实验提示完成实验,完整的程序代码如下:
#include
usingnamespacestd;
#definePI3.14159
classContainer
{
protected:
doubleradius;
public:
Container(doubler=0)
{
radius=r;
}
virtualvoidarea()=0;
virtualvoidvolume()=0;
virtualvoidprint()=0;
};
classCube:
publicContainer
{
private:
doublel;
public:
Cube(doublex)
{
l=x;
}
virtualvoidarea()
{
cout<<"Thecontainer'sareais:
"<<6*l*l<}
virtualvoidvolume()
{
cout<<"Thecontainer'svolumeis:
"<}
virtualvoidprint()
{
cout<<"Itisacube!
\n";
}
};
classSphere:
publicContainer
{
private:
doubler;
public:
Sphere(doubley)
{
r=y;
}
virtualvoidarea()
{
cout<<"Thecontainer'sareais:
"<<4*PI*r*r<}
virtualvoidvolume()
{
cout<<"Thecontainer'svolumeis:
"<<4/3*PI*r*r*r<}
virtualvoidprint()
{cout<<"Itisasphere!
\n";}
};
classCylinder:
publicContainer
{
private:
doubler;
doubleh;
public:
Cylinder(doubler1,doubleh1)
{r=r1;
h=h1;}
virtualvoidarea()
{cout<<"Thecontainer'sareais:
"<<2*PI*r*r+2*PI*r*h<virtualvoidvolume()
{cout<<"Thecontainer'svolumeis:
"<virtualvoidprint()
{cout<<"Itisacylinder!
\n";}
};
voidmain()
{Container*p;
CubeCu(4);
SphereSp(4);
CylinderCy(4,5);
p=&Cu;
p->area();
p->volume();
p->print();
p=&Sp;
p->area();
p->volume();
p->print();
p=&Cy;
p->area();
p->volume();
p->print();
}
实验报告
你在主函数中调用该函数时测试动态多态性所增加的语句是:
P=&Cu;
P=&Sp;
P=&Cy;
由①②两步,请在小结中总结关于动态多态性的实现方法。
在基类与派生类中存在的同名成员函数,定义时在形式参数的个数,顺序,类型方面有所不同,在程序编译时就能根据时机参数与形式参数的匹配情况,确定该类对象究竟调用了哪一个成员函数。
③主函数中定义一个Container类对象,请在小结中记录编译时的error和warning信息,从而得出什么结论?
纯虚函数不能被调用,因为它只有函数名,而无具体实现代码,无法实现具体的功能。
实验报告
实验题目
(2):
定义复数类Complex,有实部、虚部两个私有成员变量,在该类中定义多个重载的构造函数、定义析构函数和输出函数print,复数的输出形如12-3i,在类中重载+、-、*、/、++(分前++和后++)。
在主函数中定义复数类的对象,实现复数的各种算术运算,通过重载实现静态多态性。
实验解答:
根据实验提示完成实验,完整的程序代码如下:
#include
usingnamespacestd;
classComplex
{
private:
floatreal;
floatimag;
public:
Complex(floatr=0,floati=0)
{
real=r;
imag=i;
}
voidprint()
{
cout<}
Complexoperator*(Complexc1);
Complexoperator/(Complexc1);
Complexoperator++(int);
friendComplexoperator+(constComplex&c1,constComplex&c2);
friendComplexoperator-(constComplex&c1,constComplex&c2);
friendComplexoperator++(Complex&c1);
};
ComplexComplex:
:
operator*(Complexc1)
{
//Complexc;
real=real*c1.real;
imag=imag*c1.imag;
return*this;
}
ComplexComplex:
:
operator/(Complexc1)
{
//Complexc;
real=real/c1.real;
imag=imag/c1.imag;
return*this;
}
ComplexComplex:
:
operator++(int)
{
//Complexc(*this);
real++;
imag++;
return*this;
}
Complexoperator+(constComplex&c1,constComplex&c2)
{
Complexc;
c.real=c1.real+c2.real;
c.imag=c1.imag+c2.imag;
returnc;
}
Complexoperator-(constComplex&c1,constComplex&c2)
{
Complexc;
c.real=c1.real-c2.real;
c.imag=c1.imag-c2.imag;
returnc;
}
Complexoperator++(Complex&c1)
{
++c1.real;
++c1.imag;
returnc1;
}
intmain()
{
Complexa1(2.3,4.6),a2(3.6,2.8);
Complexa3,a4,a5,a6;
a3=a1+a2;
a4=a1-a2;
a5=a1*a2;
a6=a1/a2;
cout<<"a1=";
a1.print();
cout<<"a2=";
a2.print();
cout<<"a3=a1+a2=";
a3.print();
cout<<"a4=a1-a2=";
a4.print();
cout<<"a5=a1*a2=";
a5.print();
cout<<"a6=a1/a2=";
a6.print();
a3=++a1;
cout<<"aftera3=++a1";
cout<<"a1=";
a1.print();
cout<<"a3=";
a3.print();
a4=a2++;
cout<<"aftera4=a2++";
cout<<"a2=";
a2.print();
cout<<"a4=";
a4.print();
return0;
}
实验报告
程序的运行结果是:
A1=2.3+4.6
A2=3.6+2.8
A3=a1+a2=5.9+7.4
A4=a1-a2=-1.3+1.8
A5=a1*a2=8.28+12.88
A6=a1/a2=2.3+4.6
Aftera3=++a1a1=3.3+5.6
A3=3.3+4.6
Aftera4=a2++a2=4.6+3.8
A4=4.6+3.8
将主函数中所有以隐式方式调用重载运算符的语句改写成等效的显式调用重载运算符的语句为:
以隐式方式调用重载运算符的语句
等效的显式调用重载运算符的语句
A3=A1+A2;
A3=operator+(a1,a2)
A4=A1-A2;
A4=operator-(a1,a2)
A5=A1*A2;
A5=a1.operator*(a2)
A6=A1/A2;
A6=a1.perator/(a2)
A3=++A1;
A3=operator++(a1)
A4=A2++;
A4=a2.operator++
实验报告
四、实验小结(包括问题和解决方法、心得体会、意见与建议等)
1.在题目
(1)中由①②两步,请总结关于动态多态性的实现方法。
在基类与派生类中存在的同名成员函数,定义时在形式参数的个数,顺序,类
型方面有所不同,在程序编译时就能根据实际参数与形式参数的匹配情况,确定该
类对象究竟调用了哪个成员函数。
2.在题目
(1)③步中,在主函数中定义一个Container类对象,请记录编译时的error和warning信息,从而得出什么结论?
纯虚函数不能被调用,因为它只有函数名,而无具体实现代码,无法实现具体的
功能。
3.其它问题和解决方法:
在做第二个题时,把成员函数当作友元函数那样去重载运算符了,编译时报错说
参数过多,还没搞明白为什么,最后被老师指出来才发现,
4.心得体会:
看着教材上的例题照猫画虎竟然还出错了,有时针对不同的情况要有相应的处理
方法,不能再照着“我认为应该。
。
。
”那样做了,看来,无论算法还是语法,理解是
关键。
五、指导教师评语
成绩
批阅人
日期