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

2. Create a linked list called ShapeCollection to store the different shapes. 3.

ID: 3566513 • Letter: 2

Question

2. Create a linked list called ShapeCollection to store the different shapes.

3. Program each of the area() functions to calculate and return the area for each shape.   

4. Program each of the displayInfo() functions to display the shape (circle, square, and rectangle), base (only available for square and rectangle), height (only available for square and rectangle), radius (only available for circle), and the area of each object.  

5. Create an insertion method to capture the different shapes and then add them to the linked list. Allow users to select the different shapes and then input the necessary information for each shape. (HINT: You may add additional functions to the classes to capture these information.)

6. Create a display() function to show the different shapes in the linked list. This function will need to call displayArea() through the use of polymorphism.                                                                                                                        [5]

7. Create a menu to insert and to display the different objects in ShapeCollection.

8. Create a function to clean up the linked list in the heap memory prior to exiting the application. [5]

Define the following classes as stated in the diagram below. Shape is the base class; Rectangle and Circle are the derived classes (from Shape); and Square is a derived class of Rectangle.

Explanation / Answer

#include <iostream>
#include <iomanip>
using namespace std;

class Shape
{
public:
   virtual float area() = 0;
   virtual void displayInfo() = 0;
};

class Circle : public Shape
{
public:
   Circle(float r)
   {
       radius = r;
   }

   void setRadius(float rad)
   {
       radius = rad;
   }

   float area()
   {
       return 3.1415926f * radius * radius;
   }

   void displayInfo()
   {
       cout << fixed << setprecision(2);
       cout << "Circle Radius: " << radius << endl;
       cout << "Circle Area: " << area() << endl;
   }
private:
   float radius;
};

class Rectangle : public Shape
{
public:
   Rectangle(float b, float h)
   {
       base = b;
       height = h;
   }

   void setBase(float b)
   {
       base = b;
   }

   void setHeight(float h)
   {
       height = h;
   }

   float getBase()
   {
       return base;
   }

   float getHeight()
   {
       return height;
   }

   float area()
   {
       return base * height;
   }

   void displayInfo()
   {
       cout << fixed << setprecision(2);
       cout << "Rectangle Base: " << base << endl;
       cout << "Rectangle Height: " << height << endl;
       cout << "Rectangle Area: " << area() << endl;
   }

private:
   float base;
   float height;
};

class Square : public Rectangle
{
public:
   Square(float sz) : Rectangle(sz, sz)
   {

   }

   void setSize(float sz)
   {
       setBase(sz);
       setHeight(sz);
   }

   float area()
   {
       return getBase() * getBase();
   }

   void displayInfo()
   {
       cout << fixed << setprecision(2);
       cout << "Square Base: " << getBase() << endl;
       cout << "Square Height: " << getHeight() << endl;
       cout << "Square Area: " << area() << endl;
   }
};

class ShapeCollection
{
public:
   ShapeCollection()
   {
       root = NULL;
       tail = NULL;
   }
   ~ShapeCollection()
   {
       cleanUp();
   }

   void cleanUp()
   {
       while (root != NULL)
       {
           ShapeNode* p = root;
           root = p->next;
           delete p->shape;
           delete p;
       }
       tail = NULL;
   }

   void display()
   {
       ShapeNode* p = root;
       while (p != NULL)
       {
           p->shape->displayInfo();
           cout << endl;
           p = p->next;
       }

   }

   void insertion()
   {
       int tp;
       cout << "Choose the type of shape(1=Circle, 2=Rectangle, 3=Square): ";
       cin >> tp;

       if (tp < 1 || tp > 3)
       {
           cout << "Invalid type." << endl;
           return;
       }

       Shape* sp = NULL;
       float rad;
       float base;
       float height;

       if (tp == 1)
       {
           cout << "Enter the radius: ";
           cin >> rad;
           sp = new Circle(rad);
       }
       else if (tp == 2)
       {
           cout << "Enter the base: ";
           cin >> base;
           cout << "Enter the height: ";
           cin >> height;
           sp = new Rectangle(base, height);

       }
       else
       {
           cout << "Enter the size: ";
           cin >> base;
           sp = new Square(base);          
       }
       ShapeNode* p = new ShapeNode();
       p->shape = sp;
       p->next = NULL;
       if (tail != NULL)
           tail->next = p;
       else
           root = p;
       tail = p;
   }

private:
   struct ShapeNode
   {
       Shape* shape;
       ShapeNode* next;
   };

   ShapeNode* root;
   ShapeNode* tail;
};

int menu()
{
   cout << "1. Insert a shape" << endl;
   cout << "2. Display a shape" << endl;
   cout << "3. Exit" << endl;
   cout << "Enter 1-3: ";

   int opt = 0;
   cin >> opt;
   return opt;
}

int main()
{
   ShapeCollection slst;

   int cmd = menu();
   while (cmd != 3)
   {
       if (cmd == 1)
           slst.insertion();
       else if (cmd == 2)
           slst.display();
       else
           cout << "Invalid choice." << endl;
       cout << endl;
       cmd = menu();
   }
  
   slst.cleanUp();
   return 0;
}

Hire Me For All Your Tutoring Needs
Integrity-first tutoring: clear explanations, guidance, and feedback.
Drop an Email at
drjack9650@gmail.com
Chat Now And Get Quote