Question: Lab 6 Inheritance Overview In this assignment, you re going to create a series of classes to represent simple 2 D and 3 D shapes.
Lab Inheritance
Overview
In this assignment, youre going to create a series of classes to represent simple D and D shapes. While the classes
themselves are all simple, you will be using inheritance & the concept of polymorphism to store and use a group of these
separate classes. You will also create classes which make use of multiple inheritance, as well as private inheritance.
A note about Pi and math functions
While many languages have math libraries of varying size and complexity built into them, C includes little, if any, such
functionality. The value of PI for example, is not officially defined in any C standard, although some header files
provided by various creators of compilers do contain them.
For a standard in this assignment, you should use the following definition of PI in one of two different ways:
#define PI f Do a simple findandreplace of PI everywhere in your code
const float PI f; Create an actual variable with PI as its value.
Also, in this assignment you should use the float data type instead of double to ensure proper results.
Lastly, there exists a header file called which you can include to get access to some useful functions such as
the pow function, which lets you raise a value to an exponent and return the result.
Description
The basic idea of inheritance is that you have a base class, and you create new classes which derive from that base class.
One common use of inheritance is to reuse data and functions: if a base class defines those things, then the derived class
doesnt have to; it inherits them, so in effect, you get free code in that class.
Another use of inheritance is to define a class which serves as what other programming languages would call an interface.
In C the keyword interface doesnt exist, but some of the classes you create here will serve the same purpose. An
interface defines how a class should look on the outside.
The first three classes shown below will serve as these interfaces; all the other classes will derive, ultimately, from those
base classes. In this way, all classes in this assignment are Shapes, even if their type is Triangle, Sphere, Circle, etc. Some
types of inheritance model the Isa relationshipa triangle IS A shape. A circle IS A shape as well. The Isa model is
heavily used in relational databases.
Other types of inheritance model a hasa relationship. A car, for example, has an enginebut it isnt an engine. This
type of relationship can still be created, but the process is a bit different.
P a g e
Shape
ShapeD ShapeD
Square
Triangle
Circle
Triangular Pyramid
Cylinder
Sphere
class Shape
public :
virtual void Scale float scaleFactor ;
virtual void Display const ;
;
class ShapeD virtual public Shape
public :
virtual float Area const ;
void ShowArea const ;
virtual string GetNameD const ;
bool operator const ShapeD &rhs const ;
bool operator const ShapeD &rhs const ;
bool operator const ShapeD &rhs const ;
;
Class Hierarchy
COP Programming Fundamentals
Each of these
shapes is a
ShapeD which
is a Shape.
Thus, each of
these classes is
a Shape.
Each of these
shapes is a
ShapeD which
is a Shape.
Thus, each of
these classes is
a Shape. In
addition, these
classes will have
aD shape.
Class Description
Lets look at the first level of these classes: the Shape, ShapeD and ShapeD:
Shape is an abstract base class. Because at least one of
the functions in this class are pure virtual functions ie
virtual functions with at the end of the prototype, and
no definition This base says that all Shapes can scale,
and display their data. Each derived class can define HOW
they scale or display. Each class should multiply all of its
components by the passedin scaleFactor variable
ShapeD derives from the Shape class a D shape IS A
shape Like the Shape class, it is also an abstract base
class.
All ShapeD objects have an Area & ShowArea
function. In addition, comparison operators are
overloaded so that a ShapeD can compare itself to other
shapes to test different relations, in terms of the shapes
area. These functions will be defined only once, in this
class. All classes deriving from ShapeD will inherit them.
The definition of these functions will simply be comparing the Area of this to the Area of the incoming object greater
than, less than, or equal to respectively Due to polymorphism, you will not have to define one of these comparisons for
every possible combination of shapes, but instead you could compare the area of a circle against a triangle, square,
hexagon, etc... any class deriving from ShapeD will be able to use these functions asis
P a g e
ShapeD virtual public Shape
public :
virtual float Volume const ;
void ShowVolume const ;
virtual string GetNameD c
Step by Step Solution
There are 3 Steps involved in it
1 Expert Approved Answer
Step: 1 Unlock
Question Has Been Solved by an Expert!
Get step-by-step solutions from verified subject matter experts
Step: 2 Unlock
Step: 3 Unlock
