Advertisement
AshTurner67

Offline 2

Sep 27th, 2024 (edited)
95
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 10.28 KB | Source Code | 0 0
  1. #include <iostream>
  2. #include <cstring>
  3. #include <cmath>
  4.  
  5. using namespace std;
  6.  
  7. class Rectangle{
  8.     float length;
  9.     float width;
  10.     char* color;
  11.     public:
  12.  
  13.     Rectangle(){
  14.         length = 0, width = 0;
  15.         color = nullptr;
  16.     }
  17.     Rectangle(float l, float w, const char* str){
  18.         length = l;
  19.         width = w;
  20.         int len = strlen(str);
  21.         color = new char[len+1];
  22.         strcpy(color,str);
  23.     }
  24.     ~Rectangle(){
  25.         delete[] color;
  26.     }
  27.     float getLength(){
  28.         return length;
  29.     }
  30.     float getWidth(){
  31.         return width;
  32.     }
  33.     float getPerimeter(){
  34.         return 2 * (length + width);
  35.     }
  36.     float getArea(){
  37.         return length * width;
  38.     }
  39.     char* getColor(){
  40.         return color;
  41.     }
  42.     void setColor(const char* str){
  43.         delete[] color;
  44.         int len = strlen(str);
  45.         char* temp = new char[len+1];
  46.         strcpy(temp,str);
  47.         color = temp;
  48.     }
  49.     Rectangle(const Rectangle& r){
  50.         length = r.length;
  51.         width = r.width;
  52.         color = new char[strlen(r.color)+1];
  53.         strcpy(color,r.color);
  54.     }
  55.     Rectangle* clone(){
  56.         Rectangle* temp = new Rectangle;
  57.         temp->length = length;
  58.         temp->width = width;
  59.         temp->color = new char[strlen(color)+1];
  60.         strcpy(temp->color,color);
  61.         return temp;
  62.     }
  63. };
  64.  
  65. class Triangle{
  66.     float a;
  67.     float b;
  68.     float c;
  69.     char* color;
  70.    
  71.     public:
  72.  
  73.     Triangle(){
  74.         a = 0, b = 0, c = 0;
  75.         color = nullptr;
  76.     }
  77.     Triangle(float first, float second, float third, const char* str){
  78.         a = first;
  79.         b = second;
  80.         c = third;
  81.         int len = strlen(str);
  82.         char* temp = new char[len+1];
  83.         strcpy(temp,str);
  84.         color = temp;
  85.     }
  86.     ~Triangle(){
  87.         delete[] color;
  88.     }
  89.     float getA(){
  90.         return a;
  91.     }
  92.     float getB(){
  93.         return b;
  94.     }
  95.     float getC(){
  96.         return c;
  97.     }
  98.     float getPerimeter(){
  99.         return a + b + c;
  100.     }
  101.     float getArea(){
  102.         float s= (a + b + c) / 2;
  103.         return sqrt(s * (s - a) * (s - b) * (s - c));
  104.     }
  105.     char* getColor(){
  106.         return color;
  107.     }
  108.     void setColor(const char* str){
  109.         delete[] color;
  110.         int len = strlen(str);
  111.         color = new char[len+1];
  112.         strcpy(color,str);
  113.     }
  114.     Triangle (const Triangle& t){
  115.         a = t.a;
  116.         b = t.b;
  117.         c = t.c;
  118.         color = new char[strlen(t.color)+1];
  119.         strcpy(color,t.color);
  120.     }
  121.     Triangle* clone(){
  122.         Triangle* temp = new Triangle;
  123.         temp->a = a;
  124.         temp->b = b;
  125.         temp->c = c;
  126.         temp->color = new char[strlen(color)+1];
  127.         strcpy(temp->color,color);
  128.         return temp;
  129.     }
  130. };
  131.  
  132. class Circle{
  133.     float radius;
  134.     char* color;
  135.     public:
  136.     Circle(){
  137.         radius = 0;
  138.         color = nullptr;
  139.     }
  140.     Circle(float r, const char* str){
  141.         radius = r;
  142.         int len = strlen(str);
  143.         char* temp = new char[len+1];
  144.         strcpy(temp,str);
  145.         color = temp;
  146.     }
  147.     ~Circle(){
  148.         delete[] color;
  149.     }
  150.     float getRadius(){
  151.         return radius;
  152.     }
  153.     float getPerimeter(){
  154.         return 2 * 3.1416 * radius;
  155.     }
  156.     float getArea(){
  157.         return 3.1416 * radius *radius;
  158.     }
  159.     char* getColor(){
  160.         return color;
  161.     }
  162.     void setColor(const char* str){
  163.         delete[] color;
  164.         int len = strlen(str);
  165.         char* temp = new char[len+1];
  166.         strcpy(temp,str);
  167.         color = temp;
  168.     }
  169.     Circle (const Circle& c){
  170.         radius = c.radius;
  171.         color = new char[strlen(c.color)+1];
  172.         strcpy(color,c.color);
  173.     }
  174.     Circle* clone(){
  175.         Circle* temp = new Circle;
  176.         temp->radius = radius;
  177.         temp->color = new char[strlen(color)+1];
  178.         strcpy(temp->color,color);
  179.         return temp;
  180.     }
  181. };
  182.  
  183. class ShapeCollection{
  184.     int RectCap;
  185.     int TriCap;
  186.     int CircCap;
  187.     Rectangle** rectangleCollections;
  188.     Triangle** triangleCollections;
  189.     Circle** circleCollections;
  190.     int RectCount;
  191.     int TriCount;
  192.     int CircCount;
  193.     public:
  194.     ShapeCollection(){
  195.         RectCap = 1;
  196.         TriCap = 1;
  197.         CircCap = 1;
  198.         rectangleCollections = new Rectangle*[1];
  199.         triangleCollections = new Triangle*[1];
  200.         circleCollections = new Circle*[1];
  201.         RectCount = 0;
  202.         TriCount = 0;
  203.         CircCount = 0;
  204.     }
  205.     ~ShapeCollection(){
  206.         for(int i = 0; i < RectCount; i++)
  207.             delete rectangleCollections[i];
  208.         for(int i = 0; i < TriCount; i++)
  209.             delete triangleCollections[i];
  210.         for(int i = 0; i < CircCount; i++)
  211.             delete circleCollections[i];
  212.         delete[] rectangleCollections;
  213.         delete[] triangleCollections;
  214.         delete[] circleCollections;
  215.     }
  216.     void RectResize(){
  217.         Rectangle** temp = new Rectangle*[RectCap * 2];
  218.         for(int i = 0; i < RectCap * 2; i++){
  219.             if(i < RectCap) temp[i] = rectangleCollections[i]->clone();
  220.         }
  221.         for(int i = 0; i < RectCap; i++)
  222.             delete rectangleCollections[i];
  223.         delete[] rectangleCollections;
  224.         rectangleCollections = temp;
  225.         RectCap *= 2;
  226.         cout << "Increasing capacity of rectangles from " << RectCap / 2 << " to " << RectCap << '\n';
  227.     }
  228.     void TriResize(){
  229.         Triangle** temp = new Triangle*[TriCap * 2];
  230.         for(int i = 0; i < TriCap * 2; i++){
  231.             if(i < TriCap) temp[i] = triangleCollections[i]->clone();
  232.         }
  233.         for(int i = 0; i < TriCap; i++)
  234.             delete triangleCollections[i];
  235.         delete[] triangleCollections;
  236.         triangleCollections = temp;
  237.         TriCap *= 2;
  238.         cout << "Increasing capacity of triangles from " << TriCap / 2 << " to " << TriCap << '\n';
  239.     }
  240.     void CircResize(){
  241.         Circle** temp = new Circle*[CircCap * 2];
  242.         for(int i = 0; i < CircCap * 2; i++){
  243.             if(i < CircCap) temp[i] = circleCollections[i]->clone();
  244.         }
  245.         for(int i = 0; i < CircCap; i++)
  246.             delete circleCollections[i];      
  247.         delete[] circleCollections;
  248.         circleCollections = temp;
  249.         CircCap *= 2;
  250.         cout << "Increasing capacity of circles from " << CircCap / 2 << " to " << CircCap << '\n';
  251.     }
  252.     void add(Rectangle r){
  253.         RectCount++;
  254.         if(RectCount > RectCap) RectResize();
  255.         rectangleCollections[RectCount - 1] = r.clone();
  256.     }
  257.     void add(Triangle t){
  258.         TriCount++;
  259.         if(TriCount > TriCap) TriResize();
  260.         triangleCollections[TriCount - 1] = t.clone();
  261.     }
  262.     void add(Circle c){
  263.         CircCount++;
  264.         if(CircCount > CircCap) CircResize();
  265.         circleCollections[CircCount - 1] = c.clone();
  266.     }
  267.     int getRectCount(){
  268.         return RectCount;
  269.     }
  270.     int getTriCount(){
  271.         return TriCount;
  272.     }
  273.     int getCircCount(){
  274.         return CircCount;
  275.     }
  276.     void printRectangles(){
  277.         for(int i = 0; i < RectCount; i++){
  278.             cout << "Rectangle " << i << ": length: " << rectangleCollections[i]->getLength() << " width: " << rectangleCollections[i]->getWidth() << '\n';
  279.         }
  280.     }
  281.     void printTriangles(){
  282.         for(int i = 0; i < TriCount; i++){
  283.             cout << "Triangle " << i << ": a: " << triangleCollections[i]->getA() << " b: " << triangleCollections[i]->getB() << " c: " << triangleCollections[i]->getC() << '\n';
  284.         }
  285.     }
  286.     void printCircles(){
  287.         for(int i = 0; i < CircCount; i++){
  288.             cout << "Circle " << i << ": radius: " << circleCollections[i]->getRadius() << '\n';
  289.         }
  290.     }
  291. };
  292.  
  293. int main() {
  294.     // Create rectangle with length, width, color
  295.     Rectangle r1(10, 20, "Red");
  296.     // The Color is stored using malloc, which will be freed during destruction
  297.     cout << "Rectangle Perimeter: " << r1.getPerimeter() << endl;
  298.     cout << "Rectangle Area: " << r1.getArea() << endl;
  299.     cout << "Rectangle Color: " << r1.getColor() << endl;
  300.     // When changing the color, you need to free the memory of the old color
  301.     // and allocate new memory for the new color
  302.     r1.setColor("Yellow");
  303.     cout << "Rectangle Color: " << r1.getColor() << endl;
  304.     cout << "--------------------------------------" << endl;
  305.  
  306.     // Create triangle with a, b, c, color. (a, b, c are lengths of the sides)
  307.     Triangle t1(3, 4, 5, "Blue");
  308.     cout << "Triangle Perimeter: " << t1.getPerimeter() << endl;
  309.     cout << "Triangle Color: " << t1.getColor() << endl;
  310.     cout << "Triangle Area: " << t1.getArea() << endl;
  311.     t1.setColor("Orange");
  312.     cout << "Triangle Color: " << t1.getColor() << endl;
  313.     cout << "--------------------------------------" << endl;
  314.  
  315.     // Create circle with radius, color
  316.     Circle c1(7, "Green");
  317.     cout << "Circle Perimeter: " << c1.getPerimeter() << endl;
  318.     cout << "Circle Area: " << c1.getArea() << endl;
  319.     cout << "Circle Color: " << c1.getColor() << endl;
  320.     c1.setColor("Purple");
  321.     cout << "Circle Color: " << c1.getColor() << endl;
  322.     cout << "--------------------------------------" << endl;
  323.  
  324.     /*
  325.     When constructing the ShapeCollection class, you will create dynamic for
  326.     rectangles, triangles and circles. You have to dynamically allocate memory for this.
  327.     */
  328.     ShapeCollection shapes;
  329.     /* IMPORTANT: You need to pass the objects by value to the add functions
  330.     If you pass by value, the copy constructor will be called and the dynamically
  331.     allocated memory will be copied. So, you have to write copy contructor so that
  332.     memory allocation is properly done and no double free error occurs.
  333.     */
  334.  
  335.  
  336.     shapes.add(r1);
  337.     shapes.add(t1);
  338.     shapes.add(c1);
  339.  
  340.     Rectangle r2(15, 25, "Black");
  341.     Rectangle r3(150, 225, "Green");
  342.     shapes.add(r2);
  343.     shapes.add(r3);
  344.     Triangle t2(5, 12, 13, "White");
  345.     shapes.add(t2);
  346.  
  347.     cout << "Number of Rectangles: " << shapes.getRectCount() << endl;
  348.     cout << "Number of Triangles: " << shapes.getTriCount() << endl;
  349.     cout << "Number of Circles: " << shapes.getCircCount() << endl;
  350.     cout << "--------------------------------------" << endl;
  351.  
  352.     shapes.printRectangles();
  353.     shapes.printTriangles();
  354.     shapes.printCircles();
  355.  
  356.     return 0;
  357. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement