Note that the data type in the constructor is a double type, and if it is defined as an error, it is not easy to find the error if it is an error
Home |
Web Board |
Problemset |
Standing |
Status |
Statistics |
Problem B: Graph counting and area finding Problem B: Graph count and area time limit: 1 Sec Memory Limit: MB
Submit: 563 Solved: 360
[Submit] [Status] [Web Board] Description
Defines 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 subclass objects (that is, graphics).
2. Constructors and destructors.
3. The static member function that obtains the number of graphs is the statics int getnumofshapes (), and
4. Find the pure virtual function Getarea () of the graph area.
Class Circle is a subclass of the shape class, including:
1. Static data members used to record the number of Circle class objects (that is, circles).
2. A double type data member that represents a radius.
3. Constructors and destructors.
4. The overridden base-class function, Getarea (), is used to calculate the area of the circle, where Pi is valued at 3.14.
5. Static member function used to obtain the number of circles is the statics int getnumofcircles ().
Class Square is also a subclass of the shape class, including:
1. Static data members used to record the number of square objects (that is, squares).
2. A double type data member that represents a side length.
3. Constructors and destructors.
4. The overridden base class function, Getarea (), is used to find the area of a square.
5. Static member function for the number of squares to be used to getnumofsquares ().
Note: All static members used for the number of records only increase.
Input
Line 1th n>0, which indicates that there are n test cases.
Each test case is divided into 2 parts: The 1th part is 1 characters C or S, which means that a circle or a square is produced; the 2nd part is a real number, which is the radius of the circle or the length of the side of the square.
Output
See examples.
Where the area output is 2 decimal places.
Sample Input2C 1.1S 2.34Sample Outputnumofshapes = 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 = 1HINT Append codeappend.cc, [Submit] [Status] [Web Board]
??? < Chinese????? 中文版???
All Copyright Reserved 2010-2011 sdustoj TEAM
GPL2.0 2003-2011 hustoj Project TEAM
Anything about the problems admin:admin
#include <iostream>#include<iomanip>using namespacestd;classshape{ Public: Static intnum; Shape () {num++;cout<<"A shape is created!"<<Endl;} Virtual~shape () {cout<<"A shape is erased!"<<Endl;} Static intGetnumofshapes () {returnnum;} Virtual DoubleGetarea () =0;};classCircle: Publicshape{ Public: DoubleR; Static intNUM1; Circle (DoubleA=0): R (a) {num1++;cout<<"A Circle is created!"<<Endl;} ~circle () {cout<<"A Circle is erased!"<<Endl;} DoubleGetarea () {return 3.14*r*R;} Static intGetnumofcircles () {returnnum1;}};classSquare: Publicshape{ Public: Doubles; Static intnum2; Square (DoubleA=0): S (a) {num2++;cout<<"A Square is created!"<<Endl;} ~square () {cout<<"A Square is erased!"<<Endl;} DoubleGetarea () {returns*s;} Static intGetnumofsquares () {returnnum2;}};intshape::num=0;intcircle::num1=0;intSquare::num2=0;intMain () {intcases; Chartype; Doubledata; Shape*shape; CIN>>cases; cout<<"numofshapes ="<<shape::getnumofshapes (); cout<<", numofcircles ="<<Circle::getnumofcircles (); cout<<", numofsquares ="<<square::getnumofsquares () <<Endl; for(inti =0; i < cases; i++) {cin>>type>>data; Switch(type) { Case 'C': Shape=NewCircle (data); Break; Case 'S': Shape=NewSquare (data); Break; } cout<<"Area ="<<setprecision (2) <<fixed<<shape->getarea () <<Endl; Deleteshape; } cout<<"numofshapes ="<<shape::getnumofshapes (); cout<<", numofcircles ="<<Circle::getnumofcircles (); cout<<", numofsquares ="<<square::getnumofsquares () <<Endl;}
Experiment 12:problem B: Graph counting and calculating area