Skip to content

CS 1415 - Lab #11

Objectives:

Program with Polymorphism

Preparation:

Review Chapter 15 from Starting Out with C++ by Gaddis.

Theory:

The area of a triangle, given the length of its three sides; a, b, and c; can be calculated by:

s = (a + b + c) / 2

then

area = sqrt(s * (s - a) * (s - b) * (s - c))
Programming:
  1. Place each class in its own library. Name the library files LlllFfffClass.h and LlllFfff'Class.cpp where Llll is the first four letters of your last name and Ffff is the first four letters of your first name, and Class is the name of the class contained in the particular file. Name your driver file LlllFfffLab11.cpp. There will be 10 files in all.
  2. Create an abstract base class Shape and derived classes Circle, Triangle, and Rectangle. Create a fourth class Square that is derived from class Rectangle. In the abstract base class, supply three pure virtual functions: getArea, getPerimeter, and print. The purpose of the getArea function is to return the area of the shape. The purpose of the getPerimeter function is to return the perimeter of the shape. The purpose of the print function is to print the name of the shape (such as "circle") and the independent attributes (data members) of the shape (such as "radius: 3.2"). Represent the triangle with three sides. Represent the rectangle with a length and width.
  3. Write a polymorphic driver program.  It should create an instance of each of the four derived classes using dimensions input by the user. It should then output the name, attributes, area, and perimeter of each object.  There should be a function that receives a Shape pointer which outputs the all the information of the shape: name, dimension attributes, area, and perimeter.
  4. Verify your program works correctly.
  5. Submit your source code file to ude.wons@nosneros.htrag. Due at 8:00 am on 17 March.