Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include<iostream>
- #include<cstring>
- using namespace std;
- enum Color{RED,GREEN,BLUE};
- class Shape
- {
- protected:
- Color color;
- char id[5];
- char *description;
- void copy(const Shape &s)
- {
- this->color=s.color;
- strcpy(this->id,s.id);
- this->description= new char[strlen(s.description)+1];
- strcpy(this->description,s.description);
- }
- public:
- Shape( char *id="", char *description="",Color color =RED )
- {
- this->color=color;
- strcpy(this->id,id);
- this->description= new char[strlen(description)+1];
- strcpy(this->description,description);
- }
- virtual double area()=0;
- virtual double perimeter()=0;
- virtual void draw()=0;
- virtual char *getType()=0;
- ~Shape(){delete [] description;}
- //method void draw() that will print information for the shape in the format
- //[ID color description type: (circle/square/rectangle) perimeter area]
- virtual char *printColor() const
- {
- if(color==RED){return "RED";}
- else if(color==GREEN){return "GREEN";}
- else return "BLUE";
- }
- };
- bool operator > (Shape & left, Shape & right) {
- return left.perimeter() > right.perimeter();
- }
- class Circle : public Shape
- {
- private:
- double radius;
- public:
- Circle(char *id="", char *description="",Color color =RED, double radius=0.0)
- : Shape( id, description, color)
- {
- this->radius=radius;
- }
- Circle(const Circle &c):Shape(c)
- {
- this->radius=c.radius;
- }
- double area()
- {
- return 3.14 * radius*radius;
- }
- double perimeter()
- {
- return 2*radius *3.14;
- }
- char *getType(){
- return "circle";
- }
- void draw()
- {
- cout<<id<<" "<<printColor()<<" "<<description<<" type: "<<getType()<<" "<<perimeter()<<" "<<area()<<endl;
- }
- };
- class Square : public Shape
- {
- private:
- double a ;
- public:
- Square(char *id="", char *description="",Color color =RED, double a=0.0)
- : Shape( id, description, color)
- {
- this->a=a;
- }
- Square(const Square &s):Shape(s)
- {
- this->a=s.a;
- }
- double area()
- {
- return a*a;
- }
- double perimeter()
- {
- return 4*a;
- }
- void draw()
- {
- cout<<id<<" "<<printColor()<<" "<<description<<" type: "<<getType()<<" "<<perimeter()<<" "<<area()<<endl;
- }
- char*getType()
- {
- return "square";
- }
- };
- class Rectangle : public Shape
- {
- private:
- double a ,b;
- public:
- Rectangle(char *id="", char *description="",Color color =RED, double a=0.0, double b = 0.0)
- : Shape( id, description, color)
- {
- this->a=a;
- this->b=b;
- }
- Rectangle(const Rectangle &r):Shape(r)
- {
- this->a=r.a;
- this->b=r.b;
- }
- double area()
- {
- return a*b;
- }
- double perimeter()
- {
- return 2*(a+b);
- }
- void draw()
- {
- cout<<id<<" "<<printColor()<<" "<<description<<" type: "<<getType()<<" "<<perimeter()<<" "<<area()<<endl;
- }
- char*getType()
- {
- return "rectangle";
- }
- };
- double totalArea(Shape**shape , int n )
- {
- double sum=0.0;
- for(int i = 0 ; i< n ; i++)
- {
- sum+=shape[i]->area();
- }
- return sum;
- }
- Shape *maxPerimeter(Shape **shape, int n )
- {
- Shape*maxPerimeter=shape[0];
- for(int i = 0 ; i < n ; i++)
- {
- if(*shape[i]>*maxPerimeter){
- maxPerimeter=shape[i];
- }
- }
- return maxPerimeter;
- }
- void drawAllFromType (Shape **shape , int n, char *str)
- {
- int count = 0 , count1= 0, count2= 0;
- for(int i = 0 ; i < n ; i ++){
- Circle*c=dynamic_cast<Circle*>(shape[i]);
- if(c!=0 && strcmp(str,"circle")==0) {
- shape[i]->draw();
- count++;
- }
- Square*s=dynamic_cast<Square*>(shape[i]);
- if(s!=0 && strcmp(str,"square")==0)
- {
- shape[i]->draw();
- count1++;
- }
- Rectangle *r=dynamic_cast<Rectangle*>(shape[i]);
- if(r!=0 && strcmp(str,"rectangle")==0)
- {
- shape[i]->draw();
- count2++;
- }
- }
- if(count==0 && strcmp(str,"circle")==0)
- {
- cout<<"There are no shapes from type circle"<<endl;
- }
- else if(count1==0 && strcmp(str,"square")==0)
- {
- cout<<"There are no shapes from type square"<<endl;
- }
- else if(count2==0 && strcmp(str,"rectangle")==0)
- cout<<"There are no shapes from type rectangle"<<endl;
- }
- int main () {
- int n;
- cin>>n;
- //cout<<n;
- Shape ** shapes = new Shape * [n];
- int type;
- char id [5];
- char description[20];
- int color;
- double radius, a, b;
- for (int i=0;i<n;i++) {
- cin>>type>>id>>description>>color;
- //cout<<i;
- switch(type) {
- case 0: //circle
- cin>>radius;
- shapes[i] = new Circle (id, description, (Color) color, radius);
- break;
- case 1: //square
- cin>>a;
- shapes[i] = new Square (id, description, (Color) color, a);
- break;
- case 2: //rectangle
- cin>>a>>b;
- shapes[i] = new Rectangle (id, description, (Color) color, a, b);
- break;
- default:
- break;
- }
- }
- cout<<"Total area: "<<totalArea(shapes,n)<<endl;
- cout<<"Max perimeter of a shape is: "<<(maxPerimeter(shapes,n))->perimeter()<<endl;
- cout<<"Draw only circles"<<endl;
- drawAllFromType(shapes,n,"circle");
- cout<<"Draw only squares"<<endl;
- drawAllFromType(shapes,n,"square");
- cout<<"Draw only rectangle"<<endl;
- drawAllFromType(shapes,n,"rectangle");
- return 0;
- }
Add Comment
Please, Sign In to add comment