Skip to main content
Engineering LibreTexts

4.10: Programming Assignment- Inheritance

  • Page ID
    34667
  • \( \newcommand{\vecs}[1]{\overset { \scriptstyle \rightharpoonup} {\mathbf{#1}} } \) \( \newcommand{\vecd}[1]{\overset{-\!-\!\rightharpoonup}{\vphantom{a}\smash {#1}}} \)\(\newcommand{\id}{\mathrm{id}}\) \( \newcommand{\Span}{\mathrm{span}}\) \( \newcommand{\kernel}{\mathrm{null}\,}\) \( \newcommand{\range}{\mathrm{range}\,}\) \( \newcommand{\RealPart}{\mathrm{Re}}\) \( \newcommand{\ImaginaryPart}{\mathrm{Im}}\) \( \newcommand{\Argument}{\mathrm{Arg}}\) \( \newcommand{\norm}[1]{\| #1 \|}\) \( \newcommand{\inner}[2]{\langle #1, #2 \rangle}\) \( \newcommand{\Span}{\mathrm{span}}\) \(\newcommand{\id}{\mathrm{id}}\) \( \newcommand{\Span}{\mathrm{span}}\) \( \newcommand{\kernel}{\mathrm{null}\,}\) \( \newcommand{\range}{\mathrm{range}\,}\) \( \newcommand{\RealPart}{\mathrm{Re}}\) \( \newcommand{\ImaginaryPart}{\mathrm{Im}}\) \( \newcommand{\Argument}{\mathrm{Arg}}\) \( \newcommand{\norm}[1]{\| #1 \|}\) \( \newcommand{\inner}[2]{\langle #1, #2 \rangle}\) \( \newcommand{\Span}{\mathrm{span}}\)

    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

     

    TEST DATA:
    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


    4.10: Programming Assignment- Inheritance is shared under a CC BY-SA license and was authored, remixed, and/or curated by Patrick McClanahan.

    • Was this article helpful?