OJ Problem A: 图形计数与求面积

Problem A: 图形计数与求面积

Description:
定义三个类:Shape、Circle和Square,其中Shape为抽象类,包括:

  1. 用于记录Shape类及其子类对象(即图形)个数的静态数据成员。
  2. 构造函数与析构函数。
  3. 获得图形个数的静态成员函数 static int getNumOfShapes(),以及
  4. 求图形面积的纯虚函数getArea()。
    类Circle是Shape类的子类,包括:
  5. 用于记录Circle类对象(即圆)个数的静态数据成员。
  6. 表示半径的double类型数据成员。
  7. 构造函数和析构函数。
  8. 重写的基类函数getArea(),用于求圆的面积,其中圆周率取值为3.14。
  9. 用于获得圆个数的静态成员函数static int getNumOfCircles()。
    类Square也是Shape类的子类,包括:
  10. 用于记录Square类对象(即正方形)个数的静态数据成员。
  11. 表示边长的double类型数据成员。
  12. 构造函数和析构函数。
  13. 重写的基类函数getArea(),用于求正方形的面积。
  14. 用于获得正方形个数的静态成员函数static int getNumOfSquares()。
    注意:所有用于记录个数的静态成员只增不减。

Input:第1行N>0,表示有N个测试用例。
每个测试用例分2部分:第1部分是1个字符C或者S,表示产生一个圆或者正方形;第2部分是一个实数,是圆的半径或正方形的边长。

Output:见样例。
其中面积输出2位小数。

Sample Input:
2
C 1.1
S 2.34

Sample Output:
numOfShapes = 0, numOfCircles = 0, numOfSquares = 0
A shape is created!
A circle is created!
Area = 3.80
A circle is erased!
A shape is erased!
A shape is created!
A square is created!
Area = 5.48
A square is erased!
A shape is erased!
numOfShapes = 2, numOfCircles = 1, numOfSquares = 1

HINT
Append Code:

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;
}
#include <iostream>
#include <iomanip>
using namespace std;
class Shape
{
private:
    static int num;
public:
    Shape(){cout<<"A shape is created!"<<endl;}
    virtual ~Shape(){cout<<"A shape is erased!"<<endl;num++;}
    static int getNumOfShapes()
    {
        return num;
    }
    virtual double getArea()=0;
};
int Shape::num=0;

class Circle:public Shape
{
private:
    static int num1;
public:
    double r;
    Circle(double rr):r(rr){cout<<"A circle is created!"<<endl;num1++;}
    virtual ~Circle(){cout<<"A circle is erased!"<<endl;}
    static int getNumOfCircles()
    {
        return num1;
    }
    double getArea()
    {
        return 3.14*r*r;
    }
};
int Circle::num1=0;

class Square:public Shape
{
private:
    static int num2;
public:
    double a;
    Square(double aa):a(aa){cout<<"A square is created!"<<endl;num2++;}
    virtual ~Square(){cout<<"A square is erased!"<<endl;}
    static int getNumOfSquares()
    {
        return num2;
    }
    double getArea()
    {
        return a*a;
    }
};
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;
}
  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值