欢迎来到天天文库
浏览记录
ID:62984850
大小:275.82 KB
页数:9页
时间:2021-07-23
《最新C++实验五-多态.docx》由会员上传分享,免费在线阅读,更多相关内容在教育资源-天天文库。
1、精品资料C++实验五-多态........................................精品资料福建工程学院实验报告面向对象程序设计(C++)专业电子信息工程班级1303班座号16号姓名阮华朗日期2015.11.26........................................精品资料实验五多态程序设计一、实验目的:1、理解运算符重载的概念和实质,掌握运算符重载函数的定义方法,掌握运算符重载为类的成员函数的方法。2、掌握虚函数的定义方法及其在实现多态性中的应用,理解静态连编和动态链编的区别。二、实验时间:2015年11月26号
2、三、实验地点:福建工程学院北教1四、实验内容:1.已知基类:classBase{public:virtualvoidiam(){cout<<”Base”<usingnamespacestd;classBase{public:virt
3、ualvoidiam(){........................................精品资料cout<<"Base"<iam();p=
4、&b1;p->iam();p=&b2;p->iam();return0;}实验结果如下:........................................精品资料实验结果分析:利用虚函数可以实现C++的多态,允许将子类类型的指针赋值给父类类型的指针,实现动态联编。2.编写程序,写一个Shape抽象类,该类有求表面积(get_surface)和体积(get_volume)两个纯虚函数;由该类派生出圆柱体Cylinder,球体(Sphere),正方体(Cube),计算圆柱体,球体,正方体的表面积和体积。要求用抽象类实现。程序如下:#include5、stream>usingnamespacestd;classShape{public:virtualvoidget_surface()=0;virtualvoidget_volume()=0;};classCylinder:publicShape{public:Cylinder(doublenewR,doublenewH){radius=newR;height=newH;}voidget_surface(){cout<<"ThesurfaceoftheCylinder:"<<3.14*2*radius*(radius+height)<6、_volume()........................................精品资料{cout<<"ThevolumeoftheCylinder:"<<3.14*radius*radius*height<7、adius<8、meoftheCube:
5、stream>usingnamespacestd;classShape{public:virtualvoidget_surface()=0;virtualvoidget_volume()=0;};classCylinder:publicShape{public:Cylinder(doublenewR,doublenewH){radius=newR;height=newH;}voidget_surface(){cout<<"ThesurfaceoftheCylinder:"<<3.14*2*radius*(radius+height)<6、_volume()........................................精品资料{cout<<"ThevolumeoftheCylinder:"<<3.14*radius*radius*height<7、adius<8、meoftheCube:
6、_volume()........................................精品资料{cout<<"ThevolumeoftheCylinder:"<<3.14*radius*radius*height<7、adius<8、meoftheCube:
7、adius<8、meoftheCube:
8、meoftheCube:
此文档下载收益归作者所有