CJCoding With Joseph
15free / daySign inCreate account

Build a Shape Hierarchy

Build a polymorphic shape hierarchy from scratch. Define an abstract class Shape with an abstract method int Area(). Then define three subclasses, each with a constructor and an Area() override:
- Square(int side) with area side * side
- Rectangle(int w, int h) with area w * h
- Triangle(int b, int h) with area b * h / 2
The tester stores them in a List<Shape> and sums their areas. Do NOT write a Main method.

For Square(2), Rectangle(3, 4), and Triangle(6, 4) (areas 4, 12, 12) the program prints the output shown in Expected Output.

Expected Output:

28

= newline. The output ends with a newline — print one after the last line.

Hints4 available
Topics:
Polymorphism
1
Tab to indent (Esc then Tab to move on) · Ctrl+Enter to run · Shift+Alt+F to format · Ctrl+Space to expand shortcuts (cw, cr, fori)

Your Output

Run your code to see the output here...

Test Cases

Run your code to see test case results.