C++作业5

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

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

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

资源描述

第五章4阅读程序分析运行结果。(1)#includeiostreamusingnamespacestd;classBase{protected:intn;public:Base(intm){n=m++;}virtualvoidg1(){coutBase::g1()...nendl;g4();}virtualvoidg2(){coutBase::g2()...++nendl;g3();}virtualvoidg3(){coutBase::g3()...++nendl;g4();}virtualvoidg4(){coutBase::g4()...++nendl;}};classDerive:publicBase{intj;public:Derive(intn1,intn2):Base(n1){j=n2;}voidg1(){coutDeri::g1()...++nendl;g2();}voidg3(){coutDeri::g2()...++nendl;g4();}};voidmain(){DeriveDobj(1,0);BaseBobj=Dobj;Bobj.g1();cout----------------------endl;Base*bp=&Dobj;bp-g1();cout----------------------endl;Base&bobj2=Dobj;bobj2.g1();cout----------------------endl;Dobj.g1();}运行结果:Base::g1()...1Base::g4()...2----------------------Deri::g1()...2Base::g2()...3Deri::g2()...4Base::g4()...5----------------------Deri::g1()...6Base::g2()...7Deri::g2()...8Base::g4()...9----------------------Deri::g1()...10Base::g2()...11Deri::g2()...12Base::g4()...13结果分析:BaseBobj=Dobj定义了一个基类对象并调用拷贝构造函数,通过普通基类对象访问派生类对象时,无法体现虚函数特性,只能访问到派生类的基类子对象所以调用g1()函数,并在g1()函数中运行了g4(),输出如上结果;Base*bp=&Dobj定义了一个基类对象的指针指向派生类对象,此时访问派生类对象的虚函数时能体现虚函数特性,先调用派生类虚函数g1(),在g1()中调用从基类继承的普通函数g2(),通过g2()调用虚函数g3(),再通过g3()调用从基类继承的普通函数g4(),输出上述结果;Base&bobj2=Dobj,定义了基类对象的引用,此时访问派生类对象的虚函数时能体现虚函数特性,运行原理同上;Dobj.g1()此时仅仅定义了一个派生类对象,通过该对象进行函数调用,并未体现虚函数特性。(2)#includeiostreamusingnamespacestd;classShape{public:virtualdoublearea(){return0;}virtualvoidprint()=0;};classCircle:publicShape{protected:doubler;public:Circle(doublex):r(x){}doublearea(){return3.14*r*r;}voidprint(){coutCircle:r=r\tarea=area()endl;}};classCylinder:publicCircle{doubleh;public:Cylinder(doubler,doublex):Circle(r),h(x){}doublearea(){return2*3.14*r*r+2*3.14*h;}};voidshapeArea(Shape&s){couts.area()endl;}voidshapePrint(Shape*p){p-print();}voidmain(){Shape*s[3];s[0]=&Circle(10);s[1]=&Cylinder(20,100);for(inti=0;i2;i++){shapeArea(*s[i]);shapePrint(s[i]);}}运行结果:314Circle:r=10area=3143140Circle:r=20area=3140结果分析:Shape*s[3]定义了基类对象的指针数组,并在两个派生类中重写了一个虚函数和一个纯虚函数,shapeArea函数的参数是基类对象的引用,实现了通过基类访问虚函数,shapePrint的参数是基类对象的指针,实现了通过基类对象的指针访问虚函数。(3)#includeiostreamusingnamespacestd;classA{public:voidvirtualf(){coutf()inclassAendl;}};classB:publicA{public:voidf(){coutf()inclassBendl;}voidfb(){coutnormalfunctionfb\n;}};classC:publicA{public:voidf(){coutf()inclassCendl;}voidfc(){coutnormalfunctionfcendl;}};voidf(A*p){p-f();if(typeid(*p)==typeid(B)){B*bp=dynamic_castB*(p);bp-fb();}if(typeid(*p)==typeid(C)){C*bc=dynamic_castC*(p);bc-fc();}}voidmain(){A*pa;Bb;Cc;pa=&b;f(pa);pa=&c;f(pa);}运行结果:f()inclassBnormalfunctionfbf()inclassCnormalfunctionfc结果分析:A*pa定义了基类对象的指针,指向派生类对象b,p-f()完成了通过基类指针访问派生类的虚函数,所以输出f()inclassB;虽然pa是基类对象的指针,但由于基类中有虚函数,且该指针指向派生类对象b,所以typeid返回的是派生类类型所以if语句被执行;p即pa指向派生类对象,所以向下强制转换成功,派生类中的新增成员函数得以被执行。5、用抽象类设计计算二维平面图形面积的程序,在基类TDshape中设计纯虚函数area()和printName();area()用于计算几何图形的面积,printName()用于打印输出几何图形的类名,如Triangle类的对象就打印输出“Triangle”。每个具体形状的类则从抽象类TDshape派生,各自需要定义其独有的数据成员和成员函数,并且定义area()和printName()的具体实现代码,要求编写以TDshape为接口的函数,借以访问具体类如Triangle和Rectangle类的成员函数area(),printName()。代码:#includeiostreamusingnamespacestd;classTDshape{public:virtualdoublearea()=0;virtualvoidprintName()=0;};classTriangle:publicTDshape{private:doublewidth,height;public:doublearea(){return(width*height/2);}voidprintName(){coutTriangle:endl;}voidsetwidth(doublea=0){width=a;}voidsetheight(doublea=0){height=a;}doublegetwidth(){returnwidth;}doublegetheight(){returnheight;}};classRectangle:publicTDshape{private:doublewidth,height;public:doublearea(){return(width*height);}voidprintName(){coutRectangle:endl;}voidsetwidth(doublea=0){width=a;}voidsetheight(doublea=0){height=a;}doublegetwidth(){returnwidth;}doublegetheight(){returnheight;}};voidvpf(TDshape*p){p-area();p-printName();coutArea=p-area()endl;}intmain(){Trianglet;t.setwidth(2);t.setheight(5);t.printName();coutwidth=t.getwidth();cout\theight=t.getheight()endl;Rectangler;r.printName();r.setwidth(4);r.setheight(3);coutwidth=r.getwidth();cout\theight=r.getheight()endlendl;TDshape*p[2];p[0]=&t;p[1]=&r;for(inti=0;i2;i++)vpf(p[i]);return0;}运行结果:结果分析:没有显示调用构造函数而是用了成员函数来传递数据,感觉这样有点麻烦,但书上是这么写的,没办法,最后通过基类指针访问虚函数。6、在课堂示例(5-9抽象类)的基础上,新派生出一个三角形类,该类重载show接口,用于三角星图形的显示。在main函数中创建各个图形对象,绘制出如下图案。代码://Gramdemo.cpp#includeGraphics.h#includeiostream#includeconio.husingnamespacestd;intmain(){HWNDhwnd=getConsoleHwnd();HDChdc=GetDC(hwnd);Triangletriangle1(150,150,150,100,350,150);Rectrect1(150,150,600,300);Circlecircle1(200,350,50);Circlecircle2(550,350,50);Figure*shape[4]={&circle1,&rect1,&circle2,&triangle1};for(inti=0;i4;i++)shape[i]-show(hdc);ReleaseDC(hwnd,hdc);return0;}//Figure.h#includeWindows.hclassFigure{public:virtualvoidshow(HDC)=0;};classLocation{public:Location(intx,inty);intget_x();intget_y();protected:intx_pos,y_pos;};classPoint:publicLocation,publicFigure{public:Point(intx,inty);boolis_visible();voidshow(HDChdc);voidhide(HDChdc);voidmove_to(HDChdc,intx,inty);protected:boolvisible;};classCircle:publicPoint{public

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

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

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

×
保存成功