C++第8次实验报告

整理文档很辛苦,赏杯茶钱您下走!

免费阅读已结束,点击下载阅读编辑剩下 ...

阅读已结束,您可以下载文档离线阅读编辑

资源描述

《C语言程序设计》课程实验报告(第8次)学院:数学与计算机学院姓名:张元杰学号:201510804033专业:软件工程班级:1班同组人:实验地点:6A-2实验时间:指导教师:实验名称:多态性与虚函数所使用的开发工具及环境:硬件PC实验目的:(1)了解多态性的概念。(2)了解虚函数的作用及使用方法。(3)了解静态关联和动态关联的概念和川法。(4)了解纯虚函数和抽象类的概念和用法。实验内容:1、分析程序运行结果,掌握虚函数的使用。程序一:#includeiostreamusingnamespacestd;classONE{public:virtualvoidf(){coutONEendl;}};classTWO:publicONE{public:TWO(){coutTWOendl;}};classTHREE:publicTWO{public:virtualvoidf(){TWO::f();coutTHREE;}};voidmain(){ONEaa,*p;TWObb;//TWOTHREEcc;//TWOp=&cc;p-f();//ONETHREEcoutendl;}程序二:#includeiostreamusingnamespacestd;classBase{public:virtualvoidfn(){coutInBaseClass\n;}};classSubClass:publicBase{public:virtualvoidfn(){coutInSubClass\n;}//覆盖};voidmain(){Basebc,*p;SubClasssc;p=&bc;p-fn();//InBaseClassp=≻p-fn();//InSubClass}2、实现一个类A,在A中有两个私有的整型变量a和b,定义构造函数对a和b进行初始化,并实现成员函数geta()取得a的值和getb()取b的值。实现类B从A继承,覆盖geta(),使其返回a的2倍。主函数中声明类B对象,调用类B中的geta()并将结果输出。3、设计一个计算图形面积的类库。它的顶层是一个抽象类,并且提供相应的接口函数。抽象基类Shape,派生出Point类、矩形Rectangle、正方形Square,Point类派生出圆形Circle。要求:(1)每个类有构造函数、析构函数,并有相应的输出语句,如:“正在构造圆形”(2)能显示每个类的信息,如:输出“我是圆形”(3)能计算面积、周长(4)定义一个基类Shape类型的指针,实现动态多态(5)动态创建一个圆形对象,赋值给基类Shape类型的指针变量,程序最后delete该指针,保证析构函数的正确调用(提示:虚析构函数)(6)在主函数测试。4、设计一个基类Base,其作用是计算一个图形的面积,它只有一个公有的函数成员虚函数area。再从Base类公有派生一个三角形类Triangle和一个圆类Circle,在类Triangle和类Circle中分别定义自己的area函数,用于计算各自的面积。在主函数中设计一个Base类的对象指针,分别指向类Triangle和类Circle的对象,调用各自的area函数显示相应对象的面积。分析:用虚函数实现多态。5.将上题中的Base类改为抽象类,应用抽象类,求矩形、圆形的周长和面积。分析:将Base类的其成员函数area和perimeter声明为纯虚函数,Base就成为一个抽象类。然后再从Base派生矩形类和圆形类,分别实现Base中纯虚函数的功能.结果与分析:Sy8-1:程序一:程序二:Sy8-2:#includeiostreamusingnamespacestd;classA{public:A(inta,intb):a(a),b(b){}virtualintgeta(){returna;}friendclassB;private:inta;intb;};classB:publicA{public:B(inta,intb):A(a,b){}intgeta(){return2*a;}};intmain(){A*p=newB(2,3);coutp-geta()endl;system(pause);return0;}Sy8-3:#includeiostreamusingnamespacestd;constdoublePI=3.1415926;classShape{public:Shape(){coutConstructingShape...endl;}virtual~Shape(){coutDestructingShape...endl;}virtualvoidName(){coutShareendl;}virtualdoubleArea()=0;virtualdoubleC()=0;};classPoint:publicShape{public:Point(){coutConstructingPoint...endl;}~Point(){coutDestructingPoint...endl;}virtualvoidName(){coutPointendl;}doubleArea(){return0;}doubleC(){return0;}};classRectangle:publicShape{public:Rectangle(intl,intw):length(l),width(w){coutConstructingRectangle...endl;}~Rectangle(){coutDestructingRectangle...endl;}doubleArea(){returnlength*width;}doubleC(){return(length+width)*2;}voidName(){coutRectangleendl;}private:doublelength;doublewidth;};classSqure:publicShape{public:Squre(intl):L(l){coutConstructingSqure...endl;}~Squre(){coutDestructingSqure...endl;}voidName(){coutSqureendl;}doubleArea(){returnL*L;}doubleC(){return4*L;}private:doubleL;};classCircle:publicvirtualPoint{public:Circle(doubler):radious(r){coutConstructingCircle...endl;}~Circle(){coutDestructingCircle...endl;}voidName(){coutCircleendl;}doubleArea(){returnPI*radious*radious;}doubleC(){return2*PI*radious;}private:doubleradious;};intmain(){Shape*p;p=newPoint;p-Name();coutC:p-C()endl;coutArea:p-Area()endl;deletep;coutendl;p=newRectangle(2,3);p-Name();coutC:p-C()endl;coutArea:p-Area()endl;deletep;coutendl;p=newSqure(4);p-Name();coutC:p-C()endl;coutArea:p-Area()endl;deletep;coutendl;p=newCircle(5);p-Name();coutC:p-C()endl;coutArea:p-Area()endl;deletep;system(pause);}Sy8-4:#includeiostreamusingnamespacestd;constdoublePI=3.1415926;classBase{public:Base(){coutConstructingBase...endl;}virtual~Base(){coutDestructedBase.endl;}virtualdoubleArea()=0;};classTriangle:publicBase{public:Triangle(doublea,doubleb,doublec):a(a),b(b),c(c){coutConstructingTriangle...endl;}~Triangle(){coutDestructedTriangle.endl;}doubleArea(){returnsqrt(0.5*(a+b+c)*(0.5*(a+b+c)-a)*(0.5*(a+b+c)-b)*(0.5*(a+b+c)-c));}private:doublea,b,c;};classCircle:publicBase{public:Circle(doubler):radious(r){coutConstructingCircle...endl;}~Circle(){coutDestructedCircle.endl;}doubleArea(){returnPI*radious*radious;}private:doubleradious;};intmain(){Base*p;p=newTriangle(3,4,5);coutArea:p-Area()endl;deletep;coutendl;p=newCircle(4);coutArea:p-Area()endl;deletep;coutendl;system(pause);return0;}Sy8-5:#includeiostreamusingnamespacestd;constdoublePI=3.1415926;classBase{public:Base(){coutConstructingBase...endl;}virtual~Base(){coutDestructedBase.endl;}virtualdoubleArea()=0;virtualdoublePerimeter()=0;//周长};classTriangle:publicBase{public:Triangle(doublea,doubleb,doublec):a(a),b(b),c(c){coutConstructingTriangle...endl;}~Triangle(){coutDestructedTriangle.endl;}doubleArea(){returnsqrt(0.5*(a+b+c)*(0.5*(a+b+c)-a)*(0.5*(a+b+c)-b)*(0.5*(a+b+c)-c));}doublePerimeter(){returna+b+c;}private:doublea,b,c;};classCircle:publicBase{public:Circle(doubler):radious(r){coutConstructingCircle...endl;}~Circle(){coutDestructedCircle.endl;}doubleArea(){returnPI*radious*radious;}doublePerimeter(){return2*PI*radious;}private:doubler

1 / 10
下载文档,编辑使用

©2015-2020 m.777doc.com 三七文档.

备案号:鲁ICP备2024069028号-1 客服联系 QQ:2149211541

×
保存成功