Question: Problem 1 . Area You are creating a basic geometry calculator for different shapes, and you want it to calculate the area of each shape

Problem 1. Area
You are creating a basic geometry calculator for different shapes, and you want it to calculate the area of each shape polymorphically. Write a program using C++ that uses polymorphism to handle different shapes (Circle, Rectangle, and Triangle) through a common base class Shape.
Each derived class should implement an area() function that calculates and returns the area of the respective shape. Use virtual functions (for rea) and dynamic binding to achieve polymorphism.
Requirements
Create a base class called Shape:
It should have a pure virtual function area() that returns a double.
Create derived classes:
Circle: This class should have a double member variable for the radius. The area of a circle is calculated using the formula:
area=radius2
Rectangle: This class should have two double member variables for width and height. The area is calculated using the formula:
area=widthheight
Triangle: This class should have two double member variables for base and height. The area is calculated using the formula:
area=0.5baseheight
Use a common function to calculate areas:
Create a function called printArea that accepts a pointer or reference to Shape and calls the area() function. This function should print the type of shape and its area.
Demonstrate Polymorphism:
In main(), create instances of each shape and store pointers or references to them in a collection.Use a loop to call printArea() on each shape, demonstrating polymorphism.
Example Output
Circle with radius 5 has an area of 78.54
Rectangle with width 4 and height 5 has an area of 20.00
Triangle with base 3 and height 6 has an area of 9.00
Constraints
Use the override keyword in the derived classes for the area() function.
Use const in the area() function signatures since they do not modify the shape objects.
Use #define PI 3.14159 for the value of .

Step by Step Solution

There are 3 Steps involved in it

1 Expert Approved Answer
Step: 1 Unlock blur-text-image
Question Has Been Solved by an Expert!

Get step-by-step solutions from verified subject matter experts

Step: 2 Unlock
Step: 3 Unlock

Students Have Also Explored These Related Programming Questions!