Hi

mail@pastecode.io avatar
unknown
plain_text
9 days ago
9.2 kB
3
Indexable
Never
#include <iostream>
#include <cstring>
#include <cstdlib>
using namespace std;

class Rectangle;
class Triangle;
class Circle;
//Rectangle 
class Rectangle{
  int length;
  int width;
  char *color;
  public:
  Rectangle(){
      length=0;
      width=0;
      color=(char*)malloc(sizeof(char));
  }
   Rectangle(int l,int w,const char *col_rec){
      length=l;
      width=w;
      color=(char *)malloc(strlen(col_rec)*sizeof(char));
      strcpy(color,col_rec);
      //color=(char *)col_rec;
   }
   Rectangle(int l,int w){
    length=l;
     width=w;
   }
   Rectangle (Rectangle &newRect){
      this->length=newRect.getLen();
      this->width=newRect.getWidth();
      color=(char *)malloc(sizeof(newRect.getColor()));
      strcpy(color,newRect.getColor());
   }
   Rectangle *clone(){

      
      
      return new Rectangle(*this);
   }
   void setLen(int len){
    length=len;
   }
   void setWidth(int wid){
     width=wid;
   }
   int getLen(){
    return length;
   }
   int getWidth(){
     return width;
   }
   int getPerimeter(){
    return 2*(length+width);
   }
   int getArea(){
    return length*width;
   }
   const char * getColor(){
    return color;
   }
   void setColor(const char *col_rec){
    if(color) delete [] color;
    //color=(char *)malloc(sizeof(char)*strlen(col_rec)+1);
    color=(char *)malloc(strlen(col_rec)*sizeof(char));
      strcpy(color,col_rec);
     
   }
   ~Rectangle(){
      if(color) delete [] color;
   }
};

//Triangle
class Triangle{
  int sideA;
  int sideB;
  int sideC;
  char *color;
  public:
  Triangle(){
   color=(char*)malloc(sizeof(char));
  }
 Triangle(int a,int b,int c, const char *col_tri){
    sideA=a;
    sideB=b;
    sideC=c;
    //color=(char *)malloc(sizeof(char));
   // color=(char *)col_tri;
    color=(char *)malloc(strlen(col_tri)*sizeof(char));
      strcpy(color,col_tri);
 }
 Triangle (Triangle &newTri){
      this->sideA=newTri.getSideA();
      this->sideB=newTri.getSideB();
      this->sideC=newTri.getSideC();
      color=(char *)malloc(sizeof(newTri.getColor()));
      strcpy(color,newTri.getColor());
   }
   Triangle *clone(){

   
      
      return new Triangle(*this);
   }

 int getSideA(){
    return sideA;
 }
 int getSideB(){
    return sideB;
 }
int getSideC(){
    return sideC;
 }
 void setSides(int a,int b,int c){
    sideA=a;
    sideB=b;
    sideC=c;
 }
 

 int getPerimeter(){
    return sideA+sideB+sideC;
 }
 const char * getColor(){
    return color;
 }
 double getArea(){
    double s=(sideA+sideB+sideC)/2;
    double area=s*(s-sideA)*(s-sideB)*(s-sideC);
   return sqrt(area);
 }
 void setColor(const char *col_tri){
   // free(color);
   if(color) delete [] color;
    color=(char *)malloc(strlen(col_tri)*sizeof(char));
      strcpy(color,col_tri);
 }
~Triangle(){
    if(color) delete [] color;
}
};

//Circle
class Circle{
  int radius;
  char *color;
  double pi;
  public:
   Circle(){
      color=(char*)malloc(sizeof(char));
      pi=3.1416;
   }
   Circle(int r, const char *col_cir){
    radius=r;
    pi=3.1416;
    color=(char *)malloc(strlen(col_cir)*sizeof(char));
      strcpy(color,col_cir);
   }
   Circle (Circle &newCir){
      this->radius=newCir.getRadius();
      color=(char *)malloc(sizeof(newCir.getColor()));
      strcpy(color,newCir.getColor());
   }
   Circle *clone(){

      
      
      return new Circle(*this);
   }
   int getRadius(){
    return radius;
   }
   void setRadius(int rad){
      radius=rad;
   }
  double getPerimeter(){
     return 2*pi*radius;
  }
  double getArea(){
     return pi*radius*radius;
  }
  const char *getColor(){
    return color;
  }
  void setColor(const char *col_cir){
    if(color) delete [] color;
    //color=(char *)malloc(sizeof(char)*strlen(col_cir)+1);
    color=(char *)malloc(strlen(col_cir)*sizeof(char));
      strcpy(color,col_cir);
  }
  ~Circle(){
   if(color) delete [] color;
  }

};

//Shapecollection
class ShapeCollection{
  Rectangle **rect;
  Triangle **tri;
  Circle **cir;
  int recCnt;
  int cirCnt;
  int triCnt;
  int capRect;
  int capTri;
  int capCir;
  public:
  ShapeCollection(){
    recCnt=0;
    cirCnt=0;
    triCnt=0;
    capRect=capCir=capTri=1;
    rect=new Rectangle*[1];
    cir=new Circle*[1];
    tri=new Triangle*[1];
  }
  void resize(Rectangle **rect,int capacity){
     Rectangle **temp=new Rectangle*[capacity];
     for(int i=0;i<recCnt;i++){
      temp[i]=rect[i]->clone();
     }
     /*
     for(int i=0;i<recCnt;i++){
      delete rect[i];
      }
    delete[] rect;
    */
      rect=temp;
     cout<<"Increasing capacity of Rectangle from "<<capRect<<" to "<<capacity<<endl;
     capRect=capacity;
  }
  void resize(Triangle **tri,int capacity){
     Triangle **temp=new Triangle*[capacity];
     for(int i=0;i<triCnt;i++){
      temp[i]=tri[i]->clone();
     }
     /*for(int i=0;i<triCnt;i++){
      delete tri[i];
     }
     delete [] tri;
     */
     tri=temp;
     cout<<"Increasing capacity of Triangle from "<<capTri<<" to "<<capacity<<endl;
     capTri=capacity;
  }
  void resize(Circle **cir,int capacity){
     Circle **temp=new Circle*[capacity];
     for(int i=0;i<cirCnt;i++){
      temp[i]=cir[i]->clone();
     }
     /*
     for(int i=0;i<cirCnt;i++){
      delete cir[i];
   }
    delete [] cir;
    */
    cir=temp;
     cout<<"Increasing capacity of Circle from "<<capCir<<" to "<<capacity<<endl;
     capCir=capacity;
  }
  void add(Rectangle rec_ptr){
   if(recCnt+1==capRect) resize(rect,capRect*2);
   rect[recCnt++]=rec_ptr.clone(); 
  }
  void add(Circle cir_ptr){
   if(cirCnt+1==capCir) resize(cir,capCir*2);
   cir[cirCnt++]=cir_ptr.clone();
  }

  void add(Triangle tri_ptr){
   if(triCnt+1==capTri) resize(tri,capTri*2);
   tri[triCnt++]=tri_ptr.clone();
  }

  int getRectCount(){
      return recCnt;
  }
  int getTriCount(){
      return triCnt;
  }
  int getCircCount(){
     return cirCnt;
  }
  void printRectangles(){
    for(int i=0;i<recCnt;i++){
        cout<<"Rectangle "<<i<<": length:"<<rect[i]->getLen()<<" width:"<<rect[i]->getWidth()<<endl;
    }
    
  }
  void printTriangles(){
     for(int i=0;i<triCnt;i++){
        cout<<"Triangle "<<i<<": a:"<<tri[i]->getSideA()<<" b:"<<tri[i]->getSideB()<<" c:"<<tri[i]->getSideC()<<endl;
    }
  }
  void printCircles(){
   for(int i=0;i<cirCnt;i++){
        cout<<"Circle "<<i<<": Radius :"<<cir[i]->getRadius()<<endl;
    }
  }
  ~ShapeCollection(){
   for(int i=0;i<recCnt;i++){
      delete rect[i];
   }
   for(int i=0;i<triCnt;i++){
      delete tri[i];
   }
   for(int i=0;i<cirCnt;i++){
      delete cir[i];
   }
   delete [] rect;
   delete [] tri;
   delete [] cir;
  }
  
};
int main() {
    // Create rectangle with length, width, color
    Rectangle r1(10, 20, "Red");
    // The Color is stored using malloc, which will be freed during destruction
    cout << "Rectangle Perimeter: " << r1.getPerimeter() << endl;
    cout << "Rectangle Area: " << r1.getArea() << endl;
    cout << "Rectangle Color: " << r1.getColor() << endl;
    // When changing the color, you need to free the memory of the old color
    // and allocate new memory for the new color
    r1.setColor("Yellow");
    cout << "Rectangle Color: " << r1.getColor() << endl;
    cout << "--------------------------------------" << endl;
 
    // Create triangle with a, b, c, color. (a, b, c are lengths of the sides)
    Triangle t1(3, 4, 5, "Blue");
    cout << "Triangle Perimeter: " << t1.getPerimeter() << endl;
    cout << "Triangle Color: " << t1.getColor() << endl;
    cout << "Triangle Area: " << t1.getArea() << endl;
    t1.setColor("Orange");
    cout << "Triangle Color: " << t1.getColor() << endl;
    cout << "--------------------------------------" << endl;
 
    // Create circle with radius, color
    Circle c1(7, "Green");
    cout << "Circle Perimeter: " << c1.getPerimeter() << endl;
    cout << "Circle Area: " << c1.getArea() << endl;
    cout << "Circle Color: " << c1.getColor() << endl;
    c1.setColor("Purple");
    cout << "Circle Color: " << c1.getColor() << endl;
    cout << "--------------------------------------" << endl;
 
    /*
    When constructing the ShapeCollection class, you will create dynamic for 
    rectangles, triangles and circles. You have to dynamically allocate memory for this.
    */ 
    ShapeCollection shapes;
    /* IMPORTANT: You need to pass the objects by value to the add functions
    If you pass by value, the copy constructor will be called and the dynamically
    allocated memory will be copied. So, you have to write copy contructor so that 
    memory allocation is properly done and no double free error occurs.
    */ 
 
 
    shapes.add(r1);
    shapes.add(t1);
    shapes.add(c1);
 
    Rectangle r2(15, 25, "Black");
    Rectangle r3(150, 225, "Green");
    shapes.add(r2);
    shapes.add(r3);
    Triangle t2(5, 12, 13, "White");
    shapes.add(t2);
 
    cout << "Number of Rectangles: " << shapes.getRectCount() << endl;
    cout << "Number of Triangles: " << shapes.getTriCount() << endl;
    cout << "Number of Circles: " << shapes.getCircCount() << endl;
    cout << "--------------------------------------" << endl;
 
    shapes.printRectangles();
    shapes.printTriangles();
    shapes.printCircles();
 
    return 0;
}
Leave a Comment