Problem E: Graphic Counting and Area Finding

Problem E: Graphic Counting and Area Finding

Time Limit: 1 Sec  Memory Limit: 128 MB
Submit: 1049  Solved: 713
[Submit][Status][Web Board]

Description

Define three classes: Shape, Circle and Square, where Shape is an abstract class, including:

1. Static data members used to record the number of Shape classes and their subclasses (i.e. graphics).

2. Constructor and destructor.

3. The static member function static int getNumOfShapes() to obtain the number of graphics, and

4. Pure imaginary function getArea() for calculating graphical area.

The class Circle is a subclass of the Shape class, including:

1. Static data members used to record the number of Circle class objects (i.e. circles).

2. Data members of double type representing radius.

3. Constructors and destructors.

4. The base class function getArea() is rewritten to find the area of a circle, in which the circumference ratio is 3.14.

5. Static member function static int getNumOfCircles() for obtaining the number of circles.

The Square class is also a subclass of the Shape class, including:

1. Static data members used to record the number of Square class objects (that is, squares).

2. Data members of double type representing side lengths.

3. Constructors and destructors.

4. The overridden base class function getArea() is used to find the area of a square.

5. Static member function static int getNumOfSquares() for obtaining the number of squares.

Note: All static members used to record the number increase without decreasing.

Input

Line 1, N > 0, indicates that there are N test cases.

Each test case is divided into two parts: the first part is a character C or S, which means to produce a circle or square; the second part is a real number, which is the radius of the circle or the length of the square.

Output

See the example.

The area output is 2 decimal digits.

Sample Input

2C 1.1S 2.34

Sample Output

numOfShapes = 0, numOfCircles = 0, numOfSquares = 0A shape is created!A circle is created!Area = 3.80A circle is erased!A shape is erased!A shape is created!A square is created!Area = 5.48A square is erased!A shape is erased!numOfShapes = 2, numOfCircles = 1, numOfSquares = 1
#include <bits/stdc++.h>
using namespace std;

class Shape
{
public:
    static int num;
    Shape(){num++;cout<<"A shape is created!"<<endl;}
    static int getNumOfShapes(){return num;}
    virtual double getArea()=0;
    virtual ~Shape(){cout<<"A shape is erased!"<<endl;}
};
class Circle:public Shape
{
public:
    double r;
    static int num1;
    Circle(double r=0):r(r){num1++;cout<<"A circle is created!"<<endl;}
    double getArea(){return 3.14*r*r;}
    static int getNumOfCircles(){return num1;}
    virtual ~Circle(){cout<<"A circle is erased!"<<endl;}
};
class Square:public Shape
{
public:
    double s;
    static int num2;
    Square(double s=0):s(s){num2++;cout<<"A square is created!"<<endl;}
    double getArea(){return s*s;}
    static int getNumOfSquares(){return num2;}
    virtual ~Square(){cout<<"A square is erased!"<<endl;}
};
int Shape::num=0;
int Circle::num1=0;
int Square::num2=0;

int main()
{
    int cases;
    char type;
    double data;
    Shape *shape;
    cin>>cases;
    cout<<"numOfShapes = "<<Shape::getNumOfShapes();
    cout<<", numOfCircles = "<<Circle::getNumOfCircles();
    cout<<", numOfSquares = "<<Square::getNumOfSquares()<<endl;
    for (int i = 0; i < cases; i++)
    {
        cin>>type>>data;
        switch(type)
        {
        case 'C':
            shape = new Circle(data);
            break;
        case 'S':
            shape = new Square(data);
            break;
        }
        cout<<"Area = "<<setprecision(2)<<fixed<<shape->getArea()<<endl;
        delete shape;
    }
    cout<<"numOfShapes = "<<Shape::getNumOfShapes();
    cout<<", numOfCircles = "<<Circle::getNumOfCircles();
    cout<<", numOfSquares = "<<Square::getNumOfSquares()<<endl;
}

Added by ZimmerX on Fri, 05 Jul 2019 22:46:49 +0300