-
Notifications
You must be signed in to change notification settings - Fork 1
Workshop 08 ‐ Abstract Classes and Inheritance
Atanbori edited this page Nov 28, 2024
·
1 revision
Join workshop 8 at the dedicated LINK 💻
You are tasked with creating a hierarchy of geometric shapes using abstract classes and inheritance. You need to implement three types of shapes: Circle, Rectangle, and Triangle. Each shape should have a method to calculate its area and a method to display its information. Instructions:
-
Define an abstract base class called Shape. This class should have the following pure virtual functions:
-
double calculateArea()
: This function should be responsible for calculating the area of the shape. The derived classes will provide the implementation. -
void display()
: This function should be responsible for displaying the information about the shape.
-
-
Create three derived classes from the Shape class: Circle, Rectangle, and Triangle.
-
Implement the following methods for each derived class:
-
Circle:
- Constructor that takes the radius as a parameter.
-
double calculateArea()
to calculate the area of the circle. -
void display()
to display the circle's information.
-
Rectangle:
- Constructor that takes the length and width as parameters.
-
double calculateArea()
to calculate the area of the rectangle. -
void display()
to display the rectangle's information.
-
Triangle:
- Constructor that takes the base and height as parameters.
-
double calculateArea()
to calculate the area of the triangle. -
void display()
to display the triangle's information.
-
Note:
- Make sure to use proper encapsulation and access specifiers.
- Use appropriate data members to store the dimensions of each shape.
- Implement error handling for invalid input dimensions (e.g., negative values).
- This task encourages students to understand and apply the concepts of abstract classes, inheritance, and polymorphism.