好文档 - 专业文书写作范文服务资料分享网站

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

天下 分享 时间: 加入收藏 我要投稿 点赞

第六章课后习题答案(第二版谭浩强)

1:

//xt6-1/cpp

#include //如用VC++应改为∶#include <> using namespace std; //如用VC++应取消此行 #include \ #include \ #include \ #include \ int main()

{Cylinder cy1,,,10);

cout<<\ <<()<<\ <<\ (15); ; (5,5);

cout<<\ Point &pRef=cy1;

cout<<\ Circle &cRef=cy1;

cout<<\ return 0; }

3:解法一

#include using namespace std; class Point

{public:

Point(float a,float b):x(a),y(b){}

~Point(){cout<<\ private: float x; float y; };

class Circle:public Point {public:

Circle(float a,float b,float r):Point(a,b),radius(r){} ~Circle(){cout<<\ private: float radius; };

int main()

{Point *p=new Circle,,; delete p; return 0;

} 3:解法二

#include using namespace std; class Point {public:

Point(float a,float b):x(a),y(b){}

~Point(){cout<<\ private: float x; float y; };

class Circle:public Point {public:

Circle(int a,int b,int r):Point(a,b),radius(r){} ~Circle(){cout<<\ private: float radius; };

int main()

{Point *p=new Circle,,; Circle *pt=new Circle,,; delete pt;

return 0; } 3:解法三

#include using namespace std; class Point {public:

Point(float a,float b):x(a),y(b){}

virtual ~Point(){cout<<\ private: float x; float y; };

class Circle:public Point {public:

Circle(float a,float b,float r):Point(a,b),radius(r){}

virtual ~Circle(){cout<<\ private: float radius; };

void main()

{Point *p=new Circle,,; delete p;

} 4:

#include using namespace std; //定义抽象基类Shape class Shape {public:

virtual double area() const =0; //纯虚函数 };

//定义Circle类

class Circle:public Shape {public:

Circle(double r):radius(r){} // virtual double area() const {return *radius*radius;}; // protected:

double radius; //};

//定义Rectangle类

class Rectangle:public Shape {public:

Rectangle(double w,double h):width(w),height(h){} // virtual double area() const {return width*height;} //数

结构函数 定义虚函数 半径 结构函数 定义虚函

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

第六章课后习题答案(第二版谭浩强)1://xt6-1/cpp#include//如用VC++应改为∶#includeusingnamespacestd;//如用VC++应取消此行#include\#include\#include\#include\int
推荐度:
点击下载文档文档为doc格式
1niey0ns565ap1c1kzfj507xn0uyq600qlz
领取福利

微信扫码领取福利

微信扫码分享