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;
};