声明一个基类Shape,在此基础上派生出Rectangle和Circle,二者都有GetArea()函数计算对象的面积.
展开全部
class Shape
{
public:
virtual double GetArea()=0;
};
class Rectangle : public Shape
{
public:
Rectangle(double h, double w) : height(h), width(w){}
double GetArea(){ return height * width; }
private:
double height;
double width;
};
class Circle : public Shape
{
public:
Circle(double r):radius(r){}
double GetArea(){ return 3.14159 * radius * radius; }
private:
double radius;
};
class Square : public Rectangle
{
public:
Square(double l) : Rectangle(l, l){}
};
{
public:
virtual double GetArea()=0;
};
class Rectangle : public Shape
{
public:
Rectangle(double h, double w) : height(h), width(w){}
double GetArea(){ return height * width; }
private:
double height;
double width;
};
class Circle : public Shape
{
public:
Circle(double r):radius(r){}
double GetArea(){ return 3.14159 * radius * radius; }
private:
double radius;
};
class Square : public Rectangle
{
public:
Square(double l) : Rectangle(l, l){}
};
本回答被提问者采纳
已赞过
已踩过<
评论
收起
你对这个回答的评价是?
推荐律师服务:
若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询