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 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:
arearadius
Rectangle: This class should have two double member variables for width and height. The area is calculated using the formula:
areawidthheight
Triangle: This class should have two double member variables for base and height. The area is calculated using the formula:
areabaseheight
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 has an area of
Rectangle with width and height has an area of
Triangle with base and height has an area of
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 for the value of
Step by Step Solution
There are 3 Steps involved in it
1 Expert Approved Answer
Step: 1 Unlock
Question Has Been Solved by an Expert!
Get step-by-step solutions from verified subject matter experts
Step: 2 Unlock
Step: 3 Unlock
