Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <iostream>
- #include <cstring>
- #include <cmath>
- using namespace std;
- class Rectangle{
- float length;
- float width;
- char* color;
- public:
- Rectangle(){
- length = 0, width = 0;
- color = nullptr;
- }
- Rectangle(float l, float w, const char* str){
- length = l;
- width = w;
- int len = strlen(str);
- color = new char[len+1];
- strcpy(color,str);
- }
- ~Rectangle(){
- delete[] color;
- }
- float getLength(){
- return length;
- }
- float getWidth(){
- return width;
- }
- float getPerimeter(){
- return 2 * (length + width);
- }
- float getArea(){
- return length * width;
- }
- char* getColor(){
- return color;
- }
- void setColor(const char* str){
- delete[] color;
- int len = strlen(str);
- char* temp = new char[len+1];
- strcpy(temp,str);
- color = temp;
- }
- Rectangle(const Rectangle& r){
- length = r.length;
- width = r.width;
- color = new char[strlen(r.color)+1];
- strcpy(color,r.color);
- }
- Rectangle* clone(){
- Rectangle* temp = new Rectangle;
- temp->length = length;
- temp->width = width;
- temp->color = new char[strlen(color)+1];
- strcpy(temp->color,color);
- return temp;
- }
- };
- class Triangle{
- float a;
- float b;
- float c;
- char* color;
- public:
- Triangle(){
- a = 0, b = 0, c = 0;
- color = nullptr;
- }
- Triangle(float first, float second, float third, const char* str){
- a = first;
- b = second;
- c = third;
- int len = strlen(str);
- char* temp = new char[len+1];
- strcpy(temp,str);
- color = temp;
- }
- ~Triangle(){
- delete[] color;
- }
- float getA(){
- return a;
- }
- float getB(){
- return b;
- }
- float getC(){
- return c;
- }
- float getPerimeter(){
- return a + b + c;
- }
- float getArea(){
- float s= (a + b + c) / 2;
- return sqrt(s * (s - a) * (s - b) * (s - c));
- }
- char* getColor(){
- return color;
- }
- void setColor(const char* str){
- delete[] color;
- int len = strlen(str);
- color = new char[len+1];
- strcpy(color,str);
- }
- Triangle (const Triangle& t){
- a = t.a;
- b = t.b;
- c = t.c;
- color = new char[strlen(t.color)+1];
- strcpy(color,t.color);
- }
- Triangle* clone(){
- Triangle* temp = new Triangle;
- temp->a = a;
- temp->b = b;
- temp->c = c;
- temp->color = new char[strlen(color)+1];
- strcpy(temp->color,color);
- return temp;
- }
- };
- class Circle{
- float radius;
- char* color;
- public:
- Circle(){
- radius = 0;
- color = nullptr;
- }
- Circle(float r, const char* str){
- radius = r;
- int len = strlen(str);
- char* temp = new char[len+1];
- strcpy(temp,str);
- color = temp;
- }
- ~Circle(){
- delete[] color;
- }
- float getRadius(){
- return radius;
- }
- float getPerimeter(){
- return 2 * 3.1416 * radius;
- }
- float getArea(){
- return 3.1416 * radius *radius;
- }
- char* getColor(){
- return color;
- }
- void setColor(const char* str){
- delete[] color;
- int len = strlen(str);
- char* temp = new char[len+1];
- strcpy(temp,str);
- color = temp;
- }
- Circle (const Circle& c){
- radius = c.radius;
- color = new char[strlen(c.color)+1];
- strcpy(color,c.color);
- }
- Circle* clone(){
- Circle* temp = new Circle;
- temp->radius = radius;
- temp->color = new char[strlen(color)+1];
- strcpy(temp->color,color);
- return temp;
- }
- };
- class ShapeCollection{
- int RectCap;
- int TriCap;
- int CircCap;
- Rectangle** rectangleCollections;
- Triangle** triangleCollections;
- Circle** circleCollections;
- int RectCount;
- int TriCount;
- int CircCount;
- public:
- ShapeCollection(){
- RectCap = 1;
- TriCap = 1;
- CircCap = 1;
- rectangleCollections = new Rectangle*[1];
- triangleCollections = new Triangle*[1];
- circleCollections = new Circle*[1];
- RectCount = 0;
- TriCount = 0;
- CircCount = 0;
- }
- ~ShapeCollection(){
- for(int i = 0; i < RectCount; i++)
- delete rectangleCollections[i];
- for(int i = 0; i < TriCount; i++)
- delete triangleCollections[i];
- for(int i = 0; i < CircCount; i++)
- delete circleCollections[i];
- delete[] rectangleCollections;
- delete[] triangleCollections;
- delete[] circleCollections;
- }
- void RectResize(){
- Rectangle** temp = new Rectangle*[RectCap * 2];
- for(int i = 0; i < RectCap * 2; i++){
- if(i < RectCap) temp[i] = rectangleCollections[i]->clone();
- }
- for(int i = 0; i < RectCap; i++)
- delete rectangleCollections[i];
- delete[] rectangleCollections;
- rectangleCollections = temp;
- RectCap *= 2;
- cout << "Increasing capacity of rectangles from " << RectCap / 2 << " to " << RectCap << '\n';
- }
- void TriResize(){
- Triangle** temp = new Triangle*[TriCap * 2];
- for(int i = 0; i < TriCap * 2; i++){
- if(i < TriCap) temp[i] = triangleCollections[i]->clone();
- }
- for(int i = 0; i < TriCap; i++)
- delete triangleCollections[i];
- delete[] triangleCollections;
- triangleCollections = temp;
- TriCap *= 2;
- cout << "Increasing capacity of triangles from " << TriCap / 2 << " to " << TriCap << '\n';
- }
- void CircResize(){
- Circle** temp = new Circle*[CircCap * 2];
- for(int i = 0; i < CircCap * 2; i++){
- if(i < CircCap) temp[i] = circleCollections[i]->clone();
- }
- for(int i = 0; i < CircCap; i++)
- delete circleCollections[i];
- delete[] circleCollections;
- circleCollections = temp;
- CircCap *= 2;
- cout << "Increasing capacity of circles from " << CircCap / 2 << " to " << CircCap << '\n';
- }
- void add(Rectangle r){
- RectCount++;
- if(RectCount > RectCap) RectResize();
- rectangleCollections[RectCount - 1] = r.clone();
- }
- void add(Triangle t){
- TriCount++;
- if(TriCount > TriCap) TriResize();
- triangleCollections[TriCount - 1] = t.clone();
- }
- void add(Circle c){
- CircCount++;
- if(CircCount > CircCap) CircResize();
- circleCollections[CircCount - 1] = c.clone();
- }
- int getRectCount(){
- return RectCount;
- }
- int getTriCount(){
- return TriCount;
- }
- int getCircCount(){
- return CircCount;
- }
- void printRectangles(){
- for(int i = 0; i < RectCount; i++){
- cout << "Rectangle " << i << ": length: " << rectangleCollections[i]->getLength() << " width: " << rectangleCollections[i]->getWidth() << '\n';
- }
- }
- void printTriangles(){
- for(int i = 0; i < TriCount; i++){
- cout << "Triangle " << i << ": a: " << triangleCollections[i]->getA() << " b: " << triangleCollections[i]->getB() << " c: " << triangleCollections[i]->getC() << '\n';
- }
- }
- void printCircles(){
- for(int i = 0; i < CircCount; i++){
- cout << "Circle " << i << ": radius: " << circleCollections[i]->getRadius() << '\n';
- }
- }
- };
- 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;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement