C++面向对象程序设计第六章课后习题答案第2版谭浩强.docx
- 文档编号:26247654
- 上传时间:2023-06-17
- 格式:DOCX
- 页数:9
- 大小:15.44KB
C++面向对象程序设计第六章课后习题答案第2版谭浩强.docx
《C++面向对象程序设计第六章课后习题答案第2版谭浩强.docx》由会员分享,可在线阅读,更多相关《C++面向对象程序设计第六章课后习题答案第2版谭浩强.docx(9页珍藏版)》请在冰豆网上搜索。
C++面向对象程序设计第六章课后习题答案第2版谭浩强
第六章课后习题答案(第二版谭浩强)
1:
//xt6-1/cpp
#include
usingnamespacestd;//如用VC++应取消此行
#include"cylinder、h"
#include"point、cpp"
#include"circle、cpp"
#include"cylinder、cpp"
intmain()
{Cylindercy1(3、5,6、4,5、2,10);
cout<<"\noriginalcylinder:
\nx="< < <<",volume="< cy1、setHeight(15); cy1、setRadius(7、5); cy1、setPoint(5,5); cout<<"\nnewcylinder: \n"< Point&pRef=cy1; cout<<"\npRefasapoint: "< Circle&cRef=cy1; cout<<"\ncRefasaCircle: "< return0; } 3: 解法一 #include usingnamespacestd; classPoint {public: Point(floata,floatb): x(a),y(b){} ~Point(){cout<<"executingPointdestructor"< private: floatx; floaty; }; classCircle: publicPoint {public: Circle(floata,floatb,floatr): Point(a,b),radius(r){} ~Circle(){cout<<"executingCircledestructor"< private: floatradius; }; intmain() {Point*p=newCircle(2、5,1、8,4、5); deletep; return0; } 3: 解法二 #include usingnamespacestd; classPoint {public: Point(floata,floatb): x(a),y(b){} ~Point(){cout<<"executingPointdestructor"< private: floatx; floaty; }; classCircle: publicPoint {public: Circle(inta,intb,intr): Point(a,b),radius(r){} ~Circle(){cout<<"executingCircledestructor"< private: floatradius; }; intmain() {Point*p=newCircle(2、5,1、8,4、5); Circle*pt=newCircle(2、5,1、8,4、5); deletept; return0; } 3: 解法三 #include usingnamespacestd; classPoint {public: Point(floata,floatb): x(a),y(b){} virtual~Point(){cout<<"executingPointdestructor"< private: floatx; floaty; }; classCircle: publicPoint {public: Circle(floata,floatb,floatr): Point(a,b),radius(r){} virtual~Circle(){cout<<"executingCircledestructor"< private: floatradius; }; voidmain() {Point*p=newCircle(2、5,1、8,4、5); deletep; } 4: #include usingnamespacestd; //定义抽象基类Shape classShape {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) {cout< intmain() { Circlecircle(12、6);//建立Circle类对象circle cout<<"areaofcircle="; printArea(circle);//输出circle的面积 Rectanglerectangle(4、5,8、4);//建立Rectangle类对象rectangle cout<<"areaofrectangle="; printArea(rectangle);//输出rectangle的面积 Triangletriangle(4、5,8、4);//建立Triangle类对象 cout<<"areaoftriangle="; printArea(triangle);//输出triangle的面积 return0; } 5: #include usingnamespacestd; //定义抽象基类Shape classShape {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类对象circle Squaresquare(3、5);//建立Square类对象square Rectanglerectangle(4、5,8、4);//建立Rectangle类对象rectangle Trapezoidtrapezoid(2、0,4、5,3、2);//建立Trapezoid类对象trapezoid Triangletriangle(4、5,8、4);//建立Triangle类对象 Shape*pt[5]={&circle,&square,&rectangle,&trapezoid,&triangle}; //定义基类指针数组pt,使它每一个元素指向一个派生类对象 doubleareas=0、0;//areas为总面积 for(inti=0;i<5;i++) {areas=areas+pt[i]->area();} cout<<"totolofallareas="< return0; }
- 配套讲稿:
如PPT文件的首页显示word图标,表示该PPT已包含配套word讲稿。双击word图标可打开word文档。
- 特殊限制:
部分文档作品中含有的国旗、国徽等图片,仅作为作品整体效果示例展示,禁止商用。设计者仅对作品中独创性部分享有著作权。
- 关 键 词:
- C+ 面向 对象 程序设计 第六 课后 习题 答案 版谭浩强