Skip to content

Workshop 08 ‐ Abstract Classes and Inheritance

Atanbori edited this page Nov 28, 2024 · 1 revision

CMP9133 - Workshop 8

University of Lincoln

School of Engineering and Physical Sciences

CMP9133 – Programming Principles

Landing page

Join workshop 8 at the dedicated LINK 💻

Task 1: Implement a Geometric Shapes Hierarchy

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:

  1. 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.
  2. Create three derived classes from the Shape class: Circle, Rectangle, and Triangle.

  3. 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.

355127686-237fa00a-4564-4e53-a9bb-e44e83a76b3b

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.