Home CPSC 330

Inheritance

Objective

To become more familiar with using inheritance.

Task

For this program, you will write three classes:

The Polygon class will have a protected integer representing the number of sides it has, and a public method for calculating the area. The area method should just return zero as there is no way to calculate the area of a general polygon. It should also have a constructor for setting the number of sides.

The Rectangle and Triangle classes should each be derived from Polygon. They should set the number of sides to 4 and 3 respectively. They should also override the method to calculate the area.

The Rectangle class should have a width and a height. The Triangle class should have a base and a height. They should each have constructors as well.

You should have a fourth class that contains a main method. This method will ask the user if they want to enter a Rectangle or Triangle, create the object, and call the area function.


Details


Submitting

When your program works, email the code to ifinlay@umw.edu.

Copyright © 2018 Ian Finlayson | Licensed under a Creative Commons Attribution 4.0 International License.