#include <iostream>
#include <cstring>
#include <cmath>
using namespace std;
class Rectangle
{
int length, width;
char *color; // char pointer
public:
Rectangle(int x, int y, const char *str)
{
length = x;
width = y;
// allocating memory for color
color = (char *)malloc(sizeof(char) * 50);
strcpy(color, str);
}
Rectangle()
{
// if no parameter is given,default value
length = 0;
width = 0;
// allocating memory for color
color = (char *)malloc(sizeof(char) * 50);
strcpy(color, "NONE");
}
~Rectangle()
{
free(color);
}
int getLength()
{
return length;
}
int getWidth()
{
return width;
}
void setColor(const char *str)
{
free(color);
color = (char *)malloc(sizeof(char) * 50);
strcpy(color, str);
}
int getArea()
{
return (length * width);
}
int getPerimeter()
{
return 2 * (length + width);
}
char *getColor()
{
return color;
}
void setLength(int x)
{
length = x;
}
void setWidth(int x)
{
width = x;
}
};
class Triangle
{
int a, b, c; // every side
char *color;
public:
Triangle(int x, int y, int z, const char *str)
{
a = x;
b = y;
c = z;
// allocating memory for color
color = (char *)malloc(sizeof(char) * 50);
strcpy(color, str);
}
Triangle()
{
// if no parameter is given,default value
a = 0;
b = 0;
c = 0;
// allocating memory for color
color = (char *)malloc(sizeof(char) * 50);
strcpy(color, "NONE");
// none means no color
}
~Triangle()
{
free(color);
}
int getA()
{
return a;
}
int getB()
{
return b;
}
int getC()
{
return c;
}
float getArea()
{
int s = (a + b + c) / 2;
return sqrt(s * (s - a) * (s - b) * (s - c));
}
int getPerimeter()
{
return (a + b + c);
}
char *getColor()
{
return color;
}
void setColor(const char *str)
{
free(color);
color = (char *)malloc(sizeof(char) * 50);
strcpy(color, str);
}
void setA(int x)
{
a = x;
}
void setB(int y)
{
b = y;
}
void setC(int z)
{
c = z;
}
};
class Circle
{
int radius;
char *color;
public:
Circle(int x, const char *str)
{
// constructor
radius = x;
color = (char *)malloc(sizeof(char) * 50);
strcpy(color, str);
}
Circle()
{
// if no parameter is given,default value
radius = 0;
// allocating memory for color
color = (char *)malloc(sizeof(char) * 50);
strcpy(color, "NONE");
}
~Circle()
{
free(color);
}
int getRadius()
{
return radius;
}
int getPerimeter()
{
return (2 * 3.1416 * radius);
}
float getArea()
{
return ((3.14) * radius * radius);
}
char *getColor()
{
return color;
}
void setColor(const char *str)
{
free(color);
color = (char *)malloc(sizeof(char) * 50);
strcpy(color, str);
}
void setRadius(int x)
{
radius = x;
}
};
class ShapeCollection
{
int RecCount, CircleCount, TriCount;
// array of 100 elements for each
Rectangle r[100];
Triangle t[100];
Circle c[100];
int i, j, k;
public:
ShapeCollection()
{
// construction
i = 0;
j = 0;
k = 0;
RecCount = 0;
TriCount = 0;
CircleCount = 0;
}
void addRectangle(Rectangle &x)
{
// using setter and getter
int len = x.getLength();
r[i].setLength(len);
int wid = x.getWidth();
r[i].setWidth(wid);
i++;
RecCount++;
// increasing count
}
void addTriangle(Triangle &x)
{
// using setter and getter
int len1 = x.getA();
t[j].setA(len1);
int len2 = x.getB();
t[j].setB(len2);
int len3 = x.getC();
t[j].setC(len3);
// changing the inital value of t[j]
j++;
TriCount++;
// increasing count
}
void addCircle(Circle &x)
{
int rad = x.getRadius();
c[k].setRadius(rad);
k++;
CircleCount++;
}
int getRectCount()
{
return RecCount;
}
int getTriCount()
{
return TriCount;
}
int getCircCount()
{
return CircleCount;
}
void printRectangles()
{
for (int i = 0; i < RecCount; i++)
{
cout << "Rectangle " << i << ": ";
cout << "length: " << r[i].getLength() << " ";
cout << "Width: " << r[i].getWidth() << endl;
// printing in the desired way
}
}
void printTriangles()
{
for (int j = 0; j < TriCount; j++)
{
cout << "Triangle " << j << " : ";
cout << "a: " << t[j].getA() << " ";
cout << "b: " << t[j].getB() << " ";
cout << "c: " << t[j].getC() << endl;
}
}
void printCircles()
{
for (int k = 0; k < CircleCount; k++)
{
cout << "Circle " << k << ": ";
cout << "radius: " << c[k].getRadius() << " ";
}
cout << endl;
}
};
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 static arrays for 100
rectangles, triangles and circles. You don’t have to dynamically allocate memory for this.
*/
ShapeCollection shapes;
/* IMPORTANT: You need to pass the objects by reference to the add functions
If you pass by value, the copy constructor will be called and the dynamically
allocated memory will be copied, leading to double free errors when things go
out of scope. Once passed by reference, do not directly store the reference in
the array. Instead, copy the data from the reference to the array element.
We will see better ways to handle this in the upcoming sessions.
*/
shapes.addRectangle(r1);
shapes.addTriangle(t1);
shapes.addCircle(c1);
Rectangle r2(15, 25, "Black");
shapes.addRectangle(r2);
Triangle t2(5, 12, 13, "White");
shapes.addTriangle(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;
}