Problem B: 图形计数与求面积
来源:互联网 发布:宁波行知小学总课表 编辑:程序博客网 时间:2024/04/20 08:00
Problem B: 图形计数与求面积
Description
定义三个类:Shape、Circle和Square,其中Shape为抽象类,包括:
1. 用于记录Shape类及其子类对象(即图形)个数的静态数据成员。
2. 构造函数与析构函数。
3. 获得图形个数的静态成员函数 static int getNumOfShapes(),以及
4. 求图形面积的纯虚函数getArea()。
类Circle是Shape类的子类,包括:
1. 用于记录Circle类对象(即圆)个数的静态数据成员。
2. 表示半径的double类型数据成员。
3. 构造函数和析构函数。
4. 重写的基类函数getArea(),用于求圆的面积,其中圆周率取值为3.14。
5. 用于获得圆个数的静态成员函数static int getNumOfCircles()。
类Square也是Shape类的子类,包括:
1. 用于记录Square类对象(即正方形)个数的静态数据成员。
2. 表示边长的double类型数据成员。
3. 构造函数和析构函数。
4. 重写的基类函数getArea(),用于求正方形的面积。
5. 用于获得正方形个数的静态成员函数static int getNumOfSquares()。
注意:所有用于记录个数的静态成员只增不减。
Input
第1行N>0,表示有N个测试用例。
每个测试用例分2部分:第1部分是1个字符C或者S,表示产生一个圆或者正方形;第2部分是一个实数,是圆的半径或正方形的边长。
Output
见样例。
其中面积输出2位小数。
#include <iostream>#include <iomanip>using namespace std;class Shape {private: static int NumOfShapes;public: Shape() { NumOfShapes++; cout << "A shape is created!" << endl; } virtual~Shape() { cout << "A shape is erased!" << endl; } static int getNumOfShapes() { return NumOfShapes; } virtual double getArea() = 0;};class Circle : public Shape {private: double r; static int NumOfCircles;public: Circle(double rr = 0.0) : r(rr) { NumOfCircles++; cout << "A circle is created!" << endl; } ~Circle() { cout << "A circle is erased!" << endl; } double getArea() { return 3.14 * r * r; } static int getNumOfCircles() { return NumOfCircles; }};class Square : public Shape {private: double a; static int NumOfSquares;public: Square(double aa = 0.0) : a(aa) { NumOfSquares++; cout << "A square is created!" << endl; } ~Square() { cout << "A square is erased!" << endl; } double getArea() { return a * a; } static int getNumOfSquares() { return NumOfSquares; }};int Shape::NumOfShapes = 0;int Circle::NumOfCircles = 0;int Square::NumOfSquares = 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;}
阅读全文
1 0
- Problem B: 图形计数与求面积
- Problem B: 图形计数与求面积
- Problem B: 图形计数与求面积
- Problem B: 图形计数与求面积
- Problem E: 图形计数与求面积
- Problem G: 求图形的面积和体积
- Problem A: 求图形的面积和体积
- Problem G: 求图形的面积和体积
- 求图形面积
- 选择图形求面积
- 求图形的面积
- 使用接口求图形的周长与面积
- 求图形的最大面积
- 图形类的派生与继承(求面积与求周长)
- 求折痕组成的图形的面积
- 上机项目--选择图形求面积
- 已知图形边缘像素求内部面积
- C语言函数求图形面积
- 1.2.1.1 GNU Debugger
- 从LINQ开始之LINQ to Objects(上)
- 1.2.1.1 GNU调试器
- Problem A: 你会定义类吗?
- 基于主分量和支持向量机的人脸识别
- Problem B: 图形计数与求面积
- Windows提权的几种姿势
- get请求、post请求的区别
- 【mycat】mycat中配合mysql自增主键的使用
- 数据结构二叉树线索化
- springmvc的拦截器
- 手机上也能用的快手和火山视频在线解析下载工具
- Problem C: 判断两个圆之间的关系
- Problem D: 自定义实数及其数组