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 6 Inheritance
Overview
In this assignment, youre going to create a series of classes to represent simple 2D and 3D 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 3.14159f // Do a simple find-and-replace of PI everywhere in your code
const float PI =3.14159f; // 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
3 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 Is-a relationshipa triangle IS A shape. A circle IS A shape as well. The Is-a model is
heavily used in relational databases.
Other types of inheritance model a has-a 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.
1| P a g e
Shape
Shape2D Shape3D
Square
Triangle
Circle
Triangular Pyramid
Cylinder
Sphere
class Shape
{
public :
virtual void Scale( float scaleFactor )=0;
virtual void Display() const =0;
};
class Shape2D virtual public Shape
{
public :
virtual float Area() const =0;
void ShowArea() const ;
virtual string GetName2D() const =0;
bool operator>( const Shape2D &rhs ) const ;
bool operator<( const Shape2D &rhs ) const ;
bool operator==( const Shape2D &rhs ) const ;
};
Class Hierarchy
COP3503 Programming Fundamentals 2
Each of these
shapes is a
Shape2D, which
is a Shape.
Thus, each of
these classes is
a Shape.
Each of these
shapes is a
Shape3D, which
is a Shape.
Thus, each of
these classes is
a Shape. In
addition, these
classes will have
a2D shape.
Class Description
Lets look at the first level of these classes: the Shape, Shape2D, and Shape3D:
Shape is an abstract base class. Because at least one of
the functions in this class are pure virtual functions (i.e.
virtual functions with =0 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 passed-in scaleFactor variable).
Shape2D derives from the Shape class (a 2D shape IS A
shape). Like the Shape class, it is also an abstract base
class.
All Shape2D objects have an Area() & ShowArea()
function. In addition, comparison operators are
overloaded so that a Shape2D 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 Shape2D 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 Shape2D will be able to use these 3 functions as-is.
2| P a g e
Shape3D virtual public Shape
{
public :
virtual float Volume() const =0;
void ShowVolume() const ;
virtual string GetName3D() c

Step by Step Solution

There are 3 Steps involved in it

1 Expert Approved Answer
Step: 1 Unlock blur-text-image
Question Has Been Solved by an Expert!

Get step-by-step solutions from verified subject matter experts

Step: 2 Unlock
Step: 3 Unlock

Students Have Also Explored These Related Databases Questions!