C++面向对象程序设计第六章课后习题答案(第2版—谭浩强)(共10页).docx
《C++面向对象程序设计第六章课后习题答案(第2版—谭浩强)(共10页).docx》由会员分享,可在线阅读,更多相关《C++面向对象程序设计第六章课后习题答案(第2版—谭浩强)(共10页).docx(10页珍藏版)》请在淘文阁 - 分享文档赚钱的网站上搜索。
1、精选优质文档-倾情为你奉上第六章课后习题答案(第二版谭浩强)1:/xt6-1/cpp#include /如用VC+应改为#include using namespace std; /如用VC+应取消此行#include cylinder.h#include point.cpp#include circle.cpp#include cylinder.cppint main()Cylinder cy1(3.5,6.4,5.2,10); coutnoriginal cylinder:nx=cy1.getX(), y=cy1.getY(), r=cy1.getRadius(), h=cy1.getHe
2、ight()narea=cy1.area(), volume=cy1.volume()endl; cy1.setHeight(15); cy1.setRadius(7.5); cy1.setPoint(5,5); coutnnew cylinder:ncy1; Point &pRef=cy1; coutnpRef as a point:pRef; Circle &cRef=cy1; coutncRef as a Circle:cRef; return 0;3:解法一#include using namespace std;class Pointpublic: Point(float a,flo
3、at b):x(a),y(b) Point()coutexecuting Point destructorendl; private: float x; float y;class Circle:public Pointpublic: Circle(float a,float b,float r):Point(a,b),radius(r) Circle()coutexecuting Circle destructorendl; private: float radius;int main()Point *p=new Circle(2.5,1.8,4.5); delete p; return 0
4、;3:解法二#include using namespace std;class Pointpublic: Point(float a,float b):x(a),y(b) Point()coutexecuting Point destructorendl; private: float x; float y;class Circle:public Pointpublic:Circle(int a,int b,int r):Point(a,b),radius(r) Circle()coutexecuting Circle destructorendl; private: float radiu
5、s;int main()Point *p=new Circle(2.5,1.8,4.5); Circle *pt=new Circle(2.5,1.8,4.5); delete pt; return 0;3:解法三#include using namespace std;class Pointpublic: Point(float a,float b):x(a),y(b) virtual Point()coutexecuting Point destructorendl; private: float x; float y;class Circle:public Pointpublic:Cir
6、cle(float a,float b,float r):Point(a,b),radius(r) virtual Circle()coutexecuting Circle destructorendl; private:float radius;void main()Point *p=new Circle(2.5,1.8,4.5); delete p;4:#include using namespace std;/定义抽象基类Shapeclass Shapepublic: virtual double area() const =0; /纯虚函数;/定义Circle类class Circle
7、:public Shapepublic:Circle(double r):radius(r) /结构函数 virtual double area() const return 3.14159*radius*radius; /定义虚函数 protected: double radius; /半径;/定义Rectangle类class Rectangle:public Shapepublic: Rectangle(double w,double h):width(w),height(h) /结构函数 virtual double area() const return width*height;
- 配套讲稿:
如PPT文件的首页显示word图标,表示该PPT已包含配套word讲稿。双击word图标可打开word文档。
- 特殊限制:
部分文档作品中含有的国旗、国徽等图片,仅作为作品整体效果示例展示,禁止商用。设计者仅对作品中独创性部分享有著作权。
- 关 键 词:
- C+ 面向 对象 程序设计 第六 课后 习题 答案 谭浩强 10
限制150内