Skip to main content
Engineering LibreTexts

4.10: Programming Assignment- Inheritance

  • Page ID
  • DO NOT FORGET - first three lines:

    // Your Name
    // which class is this?
    // Assignment - Area of Shape

    Write a program that

    • defines a class named Shape with a constructor that accepts a width and height value.
    • define two sub-classes: 1) a Triangle class and 2) a Rectangle class. These 2 classes will Inherit the 2 values from the Shape class
    • In the main(), define two objects: 1) A Triangle object and 2) a Rectangle object
      • At time of creation provide the 2 values necessary
    • call the getArea() function in this two class variables.
    •  DO NOT ask for user input....

    Area of a triangle is calculated as: (height * base) / 2  (height - h and base - b in the following image)

    triangle showing height and baes for area calculation

    Area of a rectangle is calculated as: height * width (or length * width)

    Submit your .cpp file


    For an triangle with a base of 2 and a height of 5 the area would be 5
    For a rectangle with a base (length) of 5 and a base of 3 the area would be 15

    • Was this article helpful?