正文
请编写一个抽象类Shape,在此基础上派生出类Rectangle和Circle,二者都有计算对象面积的函数getArea()、计算对象周长的函数getPerim()
#include <iostream> #include <cmath> using namespace std; class Shape { public: Shape(){} ~Shape(){} public: virtual double getArea() const = 0; virtual double getPerim() const = 0; private: }; class Rectangle : public Shape { public: Rectangle(double _length, double _width) : length(_length), width(_width){} ~Rectangle(){} public: double getArea() const {return length * width;} double getPerim() const {return 2 * (length + width);} private: double length; double width; }; class Circle : public Shape { public: Circle(double _radius) : radius(_radius){} ~Circle(){} public: double getArea() const {return radius * radius * M_PI;} double getPerim() const {return 2 * M_PI * radius;} private: double radius; }; int main() { Rectangle * rec = new Rectangle(2, 3); Circle * cir = new Circle(3); cout << rec->getArea() << endl; cout << rec->getPerim() << endl; cout << cir->getArea() << endl; cout << cir->getPerim() << endl; return 0; }
输出:
6 10 28.2743 18.8496