Academic Integrity: tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.

Create a C++ project that allows the end user to create objects of different sha

ID: 3709502 • Letter: C

Question

Create a C++ project that allows the end user to create objects of different shapes (Line, Square, triangle, rectangle and circle) of varying sizes, on screen.

You will create an abstract class Shape to declare a set of generalized attributes and behaviors that apply to all derived classes

Define the relationship among classes by employing Inheritance and Composition wherever applicable. Please make sure that the data is declared as private and handled through getter and setter methods.

Create a class Canvas such that all shapes are drawn on an object of type Canvas.

The driver code will store each object in an array of type Shape. Every time a shape is added to the Canvas or removed from the Canvas, you’ll update the array.

The user will select an option from the menu:

Display Canvas Data

Add a Shape to Canvas

Delete a Shape from Canvas

Delete All

The option “Display Canvas Data” prints information of all shapes on the Canvas.

Note: You do not need to draw shapes on Screen.

(USE SEPERATING INTERFACE FROM IMPLEMENTATION TECHNIQUE)

Explanation / Answer

===========================================

Added Getter and Setter methods for all the shapes (Square, cirecle, rectangle etc)

==========================================

Sample output added and Clearly mentioned the file names

=================================================

This solution is developed in Visual Studio. So, we used   “#pragma once” pre-processor. If you developing in any other ide, may be you have to put #ifndef MACRONAME type method at start of every   *.h file.

For every shape there is *.h file and *.cpp file to differentiate the definition and implementation of code.

main.cpp file is to test the application.

If you want, you can club all the shape classes in one file and canvas and main program in other file.   

================================================================================

This solution is developed in Visual Studio. So, we used   “#pragma once” pre-processor. If you developing in any other ide, may be you have to put #ifndef MACRONAME type method at start of every   *.h file.

For every shape there is *.h file and *.cpp file to differentiate the definition and implementation of code.

main.cpp file is to test the application.

If you want, you can club all the shape classes in one file and canvas and main program in other file.   

================================================================================

shape.h

--------------------------

#pragma once

class Shape {

public:      

       // Pure Virtual function to make the Shape as abstract class

       // All the derived classes should implement these

       virtual void show() = 0;

       virtual float area() = 0;

};

================================================================================

circle.h

--------------------------

#pragma once

#include "shape.h"

class Circle : public Shape {

public:

       void show();

       float area();

       void setRadius(float);

       float getRadius();

private:

       float radius;

};

==================================================================================

line.h

------------------------

#pragma once

#include "shape.h"

class Line : public Shape {

public:

       void show();

       float area();

       void setLength(float);

       float getLength();

private:

       float length;

};

===================================================================================

square.h

--------------------------

#pragma once

#include "shape.h"

#include "rectangle.h"

class Square : public Rectangle {

public:

       void show();

       //float area(); //Not Required as we call superclass area

       void setLength(float);    

       // getLength() can be called from super class

private:

       float length;

};

====================================================

triangle.h

----------------------------------------

#pragma once

#include "shape.h"

class Triangle : public Shape {

public:

       void show();

       float area();

       void setLength(float);

       void setHight(float);

       float getLength();

       float getHight();

private:

       float length, hight;

};

========================================================

canvas.h

------------------------------------

class Canvas {

public:

       void addshape(Shape* shape); // Add shape to last;

       void deleteshape(); //Remove shape from last;

       void deleteAll();

       void display();

       Canvas(); //constructor

       ~Canvas(); //destructor

private:

       Shape *shapeobj[100]; //Array of Pointer to shape obj

       int objcount; //Object count

};

===================================================================

circle.cpp

---------------------

#include <iostream>

#include "circle.h"

using namespace std;

void Circle::show() {

       cout << "Circle" << endl;

}

float Circle::area()

{

       return 3.14 * radius * radius;

}

===========================================================

line.cpp

------------------------------------

#include <iostream>

#include "line.h"

using namespace std;

void Line::show() {

       cout << "Line" << endl;

}

float Line::area()

{

       // Just returning length. This program is demonstrative purpose only.

       return length;

}

void Line::setLength(float len) {

       length = len;

}

float Line::getLength() {

       return length;

}

===============================================================

rectangle.cpp

-------------------------------------------

#include <iostream>

#include "rectangle.h"

using namespace std;

void Rectangle::show() {

       cout << "Rectangle" << endl;

}

float Rectangle::area()

{

       return length * breadth;

}

void Rectangle::setLength(float len) {

       length = len;

}

float Rectangle::getLength() {

       return length;

}

void Rectangle::setBreadth(float b) {

       breadth = b;

}

float Rectangle::getBreadth() {

       return breadth;

}

=====================================================================

square.cpp

--------------------------------------

#include <iostream>

#include "square.h"

using namespace std;

void Square::show() {

       cout << "Square" << endl;

}

//

//float Square::area() {

//     // Calling base class area (rectangel)

//     return Rectangle::area();

//

//}

void Square::setLength(float length) {

       //As it is square we set length and breadth to length in super class

       Rectangle::setLength(length);

       Rectangle::setBreadth(length);

}

============================================================================

triangle.cpp

----------------------------------------------

#include <iostream>

#include "triangle.h"

using namespace std;

void Triangle::show() {

       cout << "Triangle" << endl;

}

void Triangle::setHight(float h) {

       hight = h;

}

void Triangle::setLength(float len) {

       length = len;

}

float Triangle::getHight() {

       return hight;

}

float Triangle::getLength() {

       return length;

}

float Triangle::area()

{

       return 1/2 * length * hight;

}

=========================================================================

canvas.cpp

--------------------------------------

#include <iostream>

#include <string>

#include "canvas.h"

#include "line.h"

#include "triangle.h"

#include "rectangle.h"

#include "square.h"

#include "circle.h"

#include "shape.h"

using namespace std;

Canvas::Canvas() {

       objcount = 0; //Initialize count with 0 objects.

}

Canvas::~Canvas() {

       deleteAll(); //free all the objects at program exit

}

// display the shapes in canvas

void Canvas::display() {

       if (objcount == 0) {

              cout << "Canvas is empty" << endl;

              return;

       }

       for (int i = 0; i < objcount; i++) {

              shapeobj[i]->show(); // Display the object.

       }

}

// Add shape to the canvas

void Canvas::addshape(Shape* shape) {

       if (objcount == 100) { // we declare the object with 100 Shape obj pointers

              cout << "The Limit for adding objects reached." << endl;

              return;

       }

       objcount++;

       shapeobj[objcount-1] = shape;

}

// delete a shape from the rear

void Canvas::deleteshape() {

       if (objcount == 0) {

              cout << "Canvas is empty" << endl;

              return;

       }

       free(shapeobj[objcount-1]); // free the last object

       objcount--; // decrease the counter.

}

//Delete all the shapes from the array.

void Canvas::deleteAll() {

       for (int i = objcount; i > 0 ; i--) {

              free(shapeobj[i-1]); // free the object.

       }

       objcount = 0; // resetting obj count.

}

========================================================================

main.cpp

----------------------------------------

// InheritanceShapes.cpp : Defines the entry point for the console application.

//

#include <iostream>

#include <string>

#include "canvas.h"

#include "line.h"

#include "triangle.h"

#include "rectangle.h"

#include "square.h"

#include "circle.h"

#include "shape.h"

using namespace std;

int main() {

       char choice;

       Canvas canvasobj;

       string shapetype;

       Shape * shapeptr;

       while (1) {

              //displayMenu();

              cout << "========================================" << endl;

              cout << "1. Display Canvas Data

                           2. Add a shape to Canvas

                           3. Delete a shape from Canvas

                           4. Delete All

                           5. Exit ";

              cout << "Enter your choice : ";

              cin >> choice;

              switch (choice) {

              case '1':

                     canvasobj.display(); //display canvaas

                     break;

              case '2':

                     getchar(); //Eat out the enter char

                     cout << "Line, Square, Rectangle, Circle, Triangle " << endl;

                     cout << "Enter Type of Shape: (Enter first letter) : ";

                     cin >> choice;

                     if (choice == 'L' || choice == 'l') {

                           shapeptr = new Line;

                           canvasobj.addshape(shapeptr);

                     }

                     else if (choice == 'S' || choice == 's') {

                           shapeptr = new Square;

                           canvasobj.addshape(shapeptr);

                     }

                     else if (choice == 'R' || choice == 'r') {

                           shapeptr = new Rectangle;

                           canvasobj.addshape(shapeptr);

                     }

                     else if (choice == 'C' || choice == 'c') {

                           shapeptr = new Circle;

                           canvasobj.addshape(shapeptr);

                     }

                     else if (choice == 'T' || choice == 't') {

                           shapeptr = new Triangle;

                           canvasobj.addshape(shapeptr);

                     }

                     break;

              case '3':   //delete a shape

                     canvasobj.deleteshape();

                     break;

              case '4': // delete all shapes

                     canvasobj.deleteAll();

                     break;

              case '5': // exit from the program

                     return 0;

              default:

                     cout << "Please Enter Valid Choice. " << endl;

              }

       }

       system("pause");

       return 0;

}

=====================================================================================

Sample Output Genereated:

========================================

1. Display Canvas Data

2. Add a shape to Canvas

3. Delete a shape from Canvas

4. Delete All

5. Exit

Enter your choice : 1

Canvas is empty

========================================

1. Display Canvas Data

2. Add a shape to Canvas

3. Delete a shape from Canvas

4. Delete All

5. Exit

Enter your choice : 2

Line, Square, Rectangle, Circle, Triangle

Enter Type of Shape: (Enter first letter) : R

========================================

1. Display Canvas Data

2. Add a shape to Canvas

3. Delete a shape from Canvas

4. Delete All

5. Exit

Enter your choice : 1

Rectangle

========================================

1. Display Canvas Data

2. Add a shape to Canvas

3. Delete a shape from Canvas

4. Delete All

5. Exit

Enter your choice : 2

Line, Square, Rectangle, Circle, Triangle

Enter Type of Shape: (Enter first letter) : C

========================================

1. Display Canvas Data

2. Add a shape to Canvas

3. Delete a shape from Canvas

4. Delete All

5. Exit

Enter your choice : 2

Line, Square, Rectangle, Circle, Triangle

Enter Type of Shape: (Enter first letter) : T

========================================

1. Display Canvas Data

2. Add a shape to Canvas

3. Delete a shape from Canvas

4. Delete All

5. Exit

Enter your choice : 1

Rectangle

Circle

Triangle

========================================

1. Display Canvas Data

2. Add a shape to Canvas

3. Delete a shape from Canvas

4. Delete All

5. Exit

Enter your choice : 3

========================================

1. Display Canvas Data

2. Add a shape to Canvas

3. Delete a shape from Canvas

4. Delete All

5. Exit

Enter your choice : 1

Rectangle

Circle

========================================

1. Display Canvas Data

2. Add a shape to Canvas

3. Delete a shape from Canvas

4. Delete All

5. Exit

Enter your choice : 4

========================================

1. Display Canvas Data

2. Add a shape to Canvas

3. Delete a shape from Canvas

4. Delete All

5. Exit

Enter your choice : 1

Canvas is empty

========================================

1. Display Canvas Data

2. Add a shape to Canvas

3. Delete a shape from Canvas

4. Delete All

5. Exit

Enter your choice :