#include using namespace std;

class Shape { public: Shape(){} virtual ~Shape(){} virtual float getArea() { return -1; } };

class Circle : public Shape { private: float radius; public: Circle(float r): radius(r){} ~Circle(){} float getArea() { return 3.14 * radius * radius; } };

class Rectangle : public Shape { private: float length, width; public: Rectangle(float l, float w): length(l), width(w){} ~Rectangle(){} float getArea() { return length * width; } };

class Square : public Rectangle { public: Square(float s): Rectangle(s, s){} ~Square(){} };

int main() { Shape * sp;

sp = new Circle(5);
cout << "The area of the Circle is " << sp->getArea () << endl;
delete sp;

sp = new Rectangle(4, 6);
cout << "The area of the Rectangle is " << sp->getArea() << endl;
delete sp;

sp = new Square(5);
cout << "The area of the Square is " << sp->getArea() << endl;
delete sp;

return 0;
3下面的程序已经定义了一个基类Shape请按要求完成以下内容:#include iostreamusing namespace std;class Shapepublic Shape ~Shape virtual float getArea return -1; ;class Circle public Shape定义Circle类;class Rectangle pub

原文地址: http://www.cveoy.top/t/topic/cz7J 著作权归作者所有。请勿转载和采集!

免费AI点我,无需注册和登录