第六章课后习题答案(第二版谭浩强)
1:
//xt6-1/cpp
#include
{Cylinder cy1,,,10);
cout<<\ <<()<<\ <<\ (15); ; (5,5);
cout<<\ Point &pRef=cy1;
cout<<\ Circle &cRef=cy1;
cout<<\ return 0; }
3:解法一
#include
{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
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
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
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;} //数
结构函数 定义虚函数 半径 结构函数 定义虚函