C++ Primer(第5版) 练习 15.22
练习 15.22 对于你在上一题中选择的类,为其添加合适的虚函数及公有成员和受保护的成员。
环境:Linux Ubuntu(云服务器)
工具:vim
代码块
class Shape {public:Shape(){}virtual ~Shape(){}virtual double getArea() = 0;
};
class Rectangle: public Shape{public:Rectangle(){}Rectangle(double l, double w): length(l), width(w){}~Rectangle(){}virtual double getArea() const override { return length * width; }protected:double length = 0.0;double width = 0.0;
};
class Circle: public Shape{public:Circle(){}Circle(double r): radius(r){}~Circle(){}virtual double getArea() const override { return 3.14 * radius * radius; }protected:double radius = 0.0;
};
class Sphere: public Shape{public:Sphere(){}Sphere(double r): radius(r){}~Sphere(){}virtual double getArea() const override { return 4 * 3.14 * radius * radius; }protected:double radius = 0.0;
};
class Cone: public Shape{public:Cone(){}Cone(double r, double l): radius(r), length(l){}~Cone(){}virtual double getArea() const override { return 3.14 * radius * radius + 3.14 * radius * length; }protected:double radius = 0.0;double length = 0.0;
};