UMBC CMSC 202 Computer Science II

Lab10: Polymorphism


Objective

In this lab you will practice implementing Polymorphism.

You will implement a heterogeneous vector (a vector with different types in it) in this lab. You will also implement three classes, one abstract class called Shape, and two subclasses.


Step 1: Implement the Shape class

Write the definition of the Shape class in Shapes.h.

Shape should have a pure virtual function named Draw. Draw should be pure virtual because you cannot "draw" an abstract shape, but you would like to force subclasses to draw it. The function should return void and be const.

Shape should have another pure virtual function named getArea. getArea should be pure virtual because you cannot calculate the area of an abstract shape, but you would like to force subclasses to define it. The function should return a double and be const.

Shape should have a virtual (not pure virtual) function called getName. getName simply returns a string that is the name of the Shape, in shape's case: "Abstract Shape" (do return "Abstract Shape" in the function). You can define this function in-line in Shape.h. The function should return a string and be const.

Hints:

Step 2: Implement the Square class

Write the definition of the Square class in Shapes.h, directly after Shape. You can put the implementation (stuff that usually goes in the .cpp) in Shapes.h if you wish.

Square should publicly inherit off of Shape.

Square's constructor takes in one parameter: a double that tells the length of a square's side. You should have a private data double that stores this length.

Square should override the three virtual functions provided in Shape:

Step 3: Implement the Triangle class

Write the definition of the Triangle class in Shapes.h, directly after Square. You can put the implementation (stuff that usually goes in the .cpp) in Shapes.h if you wish.

Triangle should publicly inherit off of Shape.

Triangle's constructor takes in two parameters: a double that tells the length of triangle's base and a double that tells the length of the triangle's height. You should have two private data doubles that store this data.

Triangle should override the three virtual functions provided in Shape:

Step 4: Implement main

In Lab10Main.cpp, start off by including Shapes.h, vector, iostream and writing "using namespace std;".
In main, do the following:
  1. Create a vector of Shape pointers (vector < Shape* >)
  2. Dynamically allocate a Square of length 4 then push_back onto the vector
  3. Dynamically allocate a Triangle with a size of 4x5 then push_back onto the vector
  4. Dynamically allocate a Square of length 5 then push_back onto the vector
  5. Dynamically allocate a Triangle with a size of 4x10 then push_back onto the vector
  6. Write a for-loop that goes over the vector and does the following for each item:

Step 5: Compile and Test

To compile: g++ -ansi -Wall Lab10Main.cpp (this may change if you have any more .cpp's)

To run: ./a.out

The output you should get (format doesn't have to match):
Square
- Area: 16
- []

Triangle
- Area: 10
- |>

Square
- Area: 25
- []

Triangle
- Area: 20
- |>

Extra Credit

2 points: Square ASCII art
In Draw() for Square, print out the square in ASCII art. For example, a square of size 3 should look like:
***
***
***

4 points: Triangle ASCII art
In Draw() for Triangle, print out the triangle (assume it is a right triangle) in ASCII art. For example, a triangle of size 3x5 should look like:
*
***
*****
The hypotenuse should be as straight as possible (think about how you can use the formula of a line: y = mx+b). Please demonstrate your beautiful art with several new triangles with interesting dimensions.