Lab 4
2020_S19/wk4/lab4.cpp
The purpose of this lab is to practice inheritance and invoking base
constructors with arguments.
For this lab you will define a base class and three derived classes that will
inherit all members with public inheritance. For each class definition, you
will write a default constructor and constructor with
argument(s). For each derived class constructor definition, you will invoke
the base's corresponding constructor. The class definitions, constructors,
and member functions you will write are as follows:
Base Class - Shape
This class will contain one protected staticallly sized cstring member
variable called 'identity'. Within the public section, this class will
contain only a constructor that will accept a constant
cstring as an argument, an inputShape and a printShape member function.
- Shape(const char* id) - constructor w/ const cstring argument: assign the
passed argument to the 'identity' member variable.
- void inputShape() - empty definition {} (**NOTE** in the future
we will learn a topic of pure virtual functions and abstract base classes;
topics related to 'polymorphism')
- void printShape() - output the 'identity' member variable.
Derived Class - Rectangle
This class will inherit members of the Shape class. In addition, this
class will contain two protected member variables 'length' and 'width'.
- Rectangle() - default constructor: this will invoke the Shape
constructor and pass string literal "Rectangle",
and zero 'length' and 'width'.
- Rectangle(const int l, const int w) - constructor w/ two const integer
arguments: invoke the
Shape constructor passing string literal "Rectangle" as an argument,
then set the passed integer values to members length and width.
- void inputShape() - redefine the Shape function and prompt the user for
two positive integer values and set the length and width accordingly.
- void printShape() - redifine the Shape function and call the Shape's
printShape function. Then output the length and width member variables. Then
call the getArea() function that will return the area of the rectangle
- int getArea() - This function will return the area of the object (length *
width).
Derived Class - Triangle
This class will inherit members of the Shape class. In addition, this
class will contain two protected member variables 'base' and 'height'.
- Triangle() - default constructor: this will invoke the Shape
constructor and pass string literal "Triangle",
and zero 'base' and 'height'.
- Triangle(const int l, const int w) - constructor w/ two const integer
arguments: invoke the Shape constructor passing string literal "Triangle"
as an argument, then set the passed integer values to members base and
height.
- void inputShape() - redefine the Shape function and prompt the user
for two positive integer values
and set the base and height accordingly.
- void printShape() - redefine the Shape function and call the Shape's
printShape function. Then output the base and height member variables. Then
call the getArea() function that will return the area value of the triangle.
- float getArea() - This function will return the area of the object
((1/2) * base * height).
Derived Class - Circle
This class will inherit members of the Shape class. In addition, this
class will contain one protected member variable 'radius'.
- Circle() - default constructor: this will invoke the Shape
constructor and pass string literal "Circle",
and zero 'radius'.
- Circle(const int r) - constructor w/ const integer
argument: invoke the Shape constructor passing string literal "Circle"
as an argument, then set the passed integer value to member radius.
- void inputShape() - redefine the Shape function and prompt the user
for a positive integer value
and set the radius accordingly.
- void printShape() - redefine the Shape function and call the Shape's
printShape function. Then output the radius member variable. Then
call the getArea() function that will return the area value of the circle.
- float getArea() - This function will return the area of the object
(3.14159 * r * r).
A partial version of main has been given to you with an example
derived class called 'Square'. Fill in the missing cases within the
switch and instantiate a Rectangle, Triangle, and Circle object
within the doWhile loop.