Description
For this computer assignment, you are to write a C++ program to define and implement
several classes for two– and three–dimensional geometric shapes. The following
flowchart illustrates the hierarchical relationship between twelve different geometric
shapes–six of them (rectangle, circle, triangle, square, right triangle, and equilateral
triangle) are two–dimensional shapes, and the remaining six (cube, box, cylinder, cone,
sphere, and tetrahedron) are three–dimensional shapes. Clearly more shapes can be
added into this flowchart if a need arises.
In this flowchart, shape indicates an abstract shape and it is represented by the abstract
class which will be the base class for your design. Definition of this class is given
in the header file in directory:
For two-dimensional shapes, put the definitions of classes to represent these shapes,
namely and in your header file
and the implementations of the member functions of these classes in your
source file For three-dimensional shapes, put the definitions of classes to
represent these shapes, namely and in your
header file and the implementations of the member functions of these classes
in your source file
For the first level of derived classes of two-dimensional shapes ( and
), use the following protected data members: and for
for and (lengths of three sides) for – all of them are type double. For
2
each of these classes, in addition to default constructor, copy constructor, assignment
operator ( ), function for overloading the append operator ( ), and destructor, provide
the following public member functions:
It returns the perimeter of a geometric shape.
It returns the area of a geometric shape.
It prints the dimensions of a geometric shape.
For the second level of derived classes for two–dimensional shapes (
and ), do not add any new data members.
For the derived classes of three–dimensional shapes, use the following private data
member: for and and nothing for the others, and provide the
following public member functions:
It returns the perimeter of a geometric shape. For the
classes and it simply returns but the class
does not have its own.
It returns the total surface area of a geometric shape.
It returns the volume of a geometric shape.
It prints the dimensions of a geometric shape, but the classes
and do not have their own.
In your source file include the header file by inserting the statement:
at the top of the file, and in your source
file include the header file by inserting the statement:
at the top of the file.
To test your program, the source file of the driver program in directory:
is available to you.
To compile the source files and link the generated object files with the system library
routines, first make a link to in directory: from your
working directory, and then execute:
For a final test of your program, execute: This will test your program
and generate the output file The correct output file, is in the same
directory with
When your program is ready, mail its source files to your TA by executing:
Use the following equations to compute perimeters, areas, and volumes of geometric
shapes, where 𝑃 is the perimeter, 𝐴 is the area, and 𝑉 is the volume of a geometric shape:
Rectangle: If 𝑙 is the length and 𝑤 is the width of a rectangle, then 𝑃 =
2(𝑙 + 𝑤) and 𝐴 = 𝑙𝑤.
3
Circle: If 𝑟 is the radius of a circle, then 𝑃 = 2𝜋𝑟 and 𝐴 = 𝜋𝑟
2
.
Triangle: If 𝑎, 𝑏, and 𝑐 are the lengths of the three sides of a triangle, then
𝑃 = 𝑎 + 𝑏 + 𝑐 and 𝐴 = √𝑘(𝑘 − 𝑎)(𝑘 − 𝑏)(𝑘 − 𝑐) for 𝑘 = 𝑃/2 (Heron’s formula).
Square: This is a special case for a rectangle, such that 𝑙 = 𝑤, so the class
can be derived from the class
Right Triangle: This is a special case for a triangle, such that 𝑐 =
√𝑎
2 + 𝑏
2
, so the class can be derived from the class
Equilateral Triangle: This is also a special case for a triangle, such
that 𝑎 = 𝑏 = 𝑐, so the class can be derived from the class
Box: The class can be derived from the class If ℎ is the height of a
box, then 𝐴 = 2𝐴0 + ℎ𝑃0 and 𝑉 = ℎ𝐴0, where 𝐴0 is the area and 𝑃0 is the
perimeter of the top of the box.
Cube: This is a special case for a box, such that 𝑙 = 𝑤 = ℎ, so the class can
be derived from the class If 𝐴0 is the area of one of the faces of a cube,
then 𝐴 = 6𝐴0 and 𝑉 = 𝑙𝐴0.
Cylinder: The class can be derived from the class If ℎ is the
height of a cylinder, then 𝐴 = 2𝐴0 + 𝐴1 and 𝑉 = ℎ𝐴0, where 𝐴0 is the area of the
base and 𝐴1 is the area of the lateral surface of the cylinder. If 𝑃0 is the perimeter
of the base of the cylinder, then 𝐴1 = ℎ𝑃0.
Cone: The class can be derived from the class If 𝑟 is the radius of
the base and ℎ is the height of a cone, then 𝐴 = 𝐴0 + 𝐴1 and 𝑉 =
1
3
ℎ𝐴0, where 𝐴0
is the area of the base and 𝐴1 is the area of the lateral surface of the cone. If 𝑃0 is
the perimeter of the base and 𝑠 is the slant height of the cone, then 𝐴1 =
1
2
𝑠𝑃0
where 𝑠 = √𝑟
2 + ℎ
2 .
Sphere: The class can be derived from the class If 𝑟 is the
radius of a sphere, then 𝐴 = 4𝐴0, 𝑉 =
4
3
𝑟𝐴0, and 𝑃 = 𝑃0, where 𝐴0 is the area and
𝑃0 is the perimeter of the largest cross section of the sphere.
Tetrahedron: It is a pyramid with four equal faces and its each face is an
equilateral triangle, so the class can be derived from the class
If 𝑎 is the length of a side of a tetrahedron, then 𝐴 = 4𝐴0 and 𝑉 =
1
3
ℎ𝐴0, where 𝐴0 is the area of one of the faces of the tetrahedron and ℎ = √
2
3
𝑎.
Note: When you write the implementation of a member function of a derived class for a
geometric shape, you must use the member functions of its base class if they are
applicable. This is the basic idea of inheritance.
4
For your computations, define the value π as follows:
where is a trigonometric function, and to use this function, you need to
include the system header file in your program files.