C++面向对象程序设计第六章课后习题答案(第2版—谭浩强)

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

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

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

资源描述

第六章课后习题答案(第二版谭浩强)1://xt6-1/cpp#includeiostream//如用VC++应改为∶#includeiosttram.husingnamespacestd;//如用VC++应取消此行#includecylinder.h#includepoint.cpp#includecircle.cpp#includecylinder.cppintmain(){Cylindercy1(3.5,6.4,5.2,10);cout\noriginalcylinder:\nx=cy1.getX(),y=cy1.getY(),r=cy1.getRadius(),h=cy1.getHeight()\narea=cy1.area(),volume=cy1.volume()endl;cy1.setHeight(15);cy1.setRadius(7.5);cy1.setPoint(5,5);cout\nnewcylinder:\ncy1;Point&pRef=cy1;cout\npRefasapoint:pRef;Circle&cRef=cy1;cout\ncRefasaCircle:cRef;return0;}3:解法一#includeiostreamusingnamespacestd;classPoint{public:Point(floata,floatb):x(a),y(b){}~Point(){coutexecutingPointdestructorendl;}private:floatx;floaty;};classCircle:publicPoint{public:Circle(floata,floatb,floatr):Point(a,b),radius(r){}~Circle(){coutexecutingCircledestructorendl;}private:floatradius;};intmain(){Point*p=newCircle(2.5,1.8,4.5);deletep;return0;}3:解法二#includeiostreamusingnamespacestd;classPoint{public:Point(floata,floatb):x(a),y(b){}~Point(){coutexecutingPointdestructorendl;}private:floatx;floaty;};classCircle:publicPoint{public:Circle(inta,intb,intr):Point(a,b),radius(r){}~Circle(){coutexecutingCircledestructorendl;}private:floatradius;};intmain(){Point*p=newCircle(2.5,1.8,4.5);Circle*pt=newCircle(2.5,1.8,4.5);deletept;return0;}3:解法三#includeiostreamusingnamespacestd;classPoint{public:Point(floata,floatb):x(a),y(b){}virtual~Point(){coutexecutingPointdestructorendl;}private:floatx;floaty;};classCircle:publicPoint{public:Circle(floata,floatb,floatr):Point(a,b),radius(r){}virtual~Circle(){coutexecutingCircledestructorendl;}private:floatradius;};voidmain(){Point*p=newCircle(2.5,1.8,4.5);deletep;}4:#includeiostreamusingnamespacestd;//定义抽象基类ShapeclassShape{public:virtualdoublearea()const=0;//纯虚函数};//定义Circle类classCircle:publicShape{public:Circle(doubler):radius(r){}//结构函数virtualdoublearea()const{return3.14159*radius*radius;};//定义虚函数protected:doubleradius;//半径};//定义Rectangle类classRectangle:publicShape{public:Rectangle(doublew,doubleh):width(w),height(h){}//结构函数virtualdoublearea()const{returnwidth*height;}//定义虚函数protected:doublewidth,height;//宽与高};classTriangle:publicShape{public:Triangle(doublew,doubleh):width(w),height(h){}//结构函数virtualdoublearea()const{return0.5*width*height;}//定义虚函数protected:doublewidth,height;//宽与高};//输出面积的函数voidprintArea(constShape&s){couts.area()endl;}//输出s的面积intmain(){Circlecircle(12.6);//建立Circle类对象circlecoutareaofcircle=;printArea(circle);//输出circle的面积Rectanglerectangle(4.5,8.4);//建立Rectangle类对象rectanglecoutareaofrectangle=;printArea(rectangle);//输出rectangle的面积Triangletriangle(4.5,8.4);//建立Triangle类对象coutareaoftriangle=;printArea(triangle);//输出triangle的面积return0;}5:#includeiostreamusingnamespacestd;//定义抽象基类ShapeclassShape{public:virtualdoublearea()const=0;//纯虚函数};//定义Circle(圆形)类classCircle:publicShape{public:Circle(doubler):radius(r){}//结构函数virtualdoublearea()const{return3.14159*radius*radius;};//定义虚函数protected:doubleradius;//半径};//定义Square(正方形)类classSquare:publicShape{public:Square(doubles):side(s){}//结构函数virtualdoublearea()const{returnside*side;}//定义虚函数protected:doubleside;};//定义Rectangle(矩形)类classRectangle:publicShape{public:Rectangle(doublew,doubleh):width(w),height(h){}//结构函数virtualdoublearea()const{returnwidth*height;}//定义虚函数protected:doublewidth,height;//宽与高};//定义Trapezoid(梯形)类classTrapezoid:publicShape{public:Trapezoid(doublet,doubleb,doubleh):top(t),bottom(t),height(h){}//结构函数virtualdoublearea()const{return0.5*(top+bottom)*height;}//定义虚函数protected:doubletop,bottom,height;//上底、下底与高};//定义Triangle(三角形)类classTriangle:publicShape{public:Triangle(doublew,doubleh):width(w),height(h){}//结构函数virtualdoublearea()const{return0.5*width*height;}//定义虚函数protected:doublewidth,height;//宽与高};intmain(){Circlecircle(12.6);//建立Circle类对象circleSquaresquare(3.5);//建立Square类对象squareRectanglerectangle(4.5,8.4);//建立Rectangle类对象rectangleTrapezoidtrapezoid(2.0,4.5,3.2);//建立Trapezoid类对象trapezoidTriangletriangle(4.5,8.4);//建立Triangle类对象Shape*pt[5]={&circle,&square,&rectangle,&trapezoid,&triangle};//定义基类指针数组pt,使它每一个元素指向一个派生类对象doubleareas=0.0;//areas为总面积for(inti=0;i5;i++){areas=areas+pt[i]-area();}couttotolofallareas=areasendl;//输出总面积return0;}

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

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

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

×
保存成功