Using C++ Classes for Shape Calculations
The purpose of this assignment is to write a set of classes that are used by a main program. In
main(), you declare objects of each of the class-types you define. The program produces the
output below by calling methods for the objects/variables. The classes you are writing are all
very similar, but they each must have the implementation and interface code written for them in
their corresponding *.cpp and *.h files.
The main() function is in the homework6.cpp file,
which is provided as part of the assignment on Moodle.
Additionally, skeletons for the
cylinder.h and cylinder.cpp files have also been provided.
Each shape class must contain 4
methods:
a constructor, volume(), surface_area(), and write().
Problem Statement:
Design and implement five classes called
Cylinder
,
Sphere
,
Prism
,
Cone
, and
Pyramid
. The pyramid is
square-based
, and the prism is
rectangular
. The methods
defined for these classes are called from main(). You must create and submit:
•
cylinder.cpp & cylinder.h,
•
sphere.cpp & sphere.h,
•
prism.cpp & prism.h,
•
cone.cpp & cone.h, and
•
pyramid.cpp & pyramid.h
Learning Objectives
•
Writing class definitions
•
Understanding and using separate compilation
•
Using multiple files to contain multiple classes
•
Using header files to contain class declarations and .cpp files to hold class definitions
•
Calling class methods
Sample Input/Output
:
Your program will be tested with several input sets, including
the one shown here in
RED
, and it must match the given format!
Enter cylinder height and radius >>>
6.25 3.5
The cylinder volume is 240.53
The cylinder surface area is 214.41
CYLINDER: 6.25, 3.50
Enter sphere radius >>>
16.28
The sphere volume is 18073.90
The sphere surface area is 3330.57
SPHERE: 16.28
Enter rectangular prism base length, height, and width >>>
4.04 7.79 3.25
The rectangular prism volume is 102.28
The rectangular prism surface area is 139.84
PRISM: 4.04, 7.79, 3.25

Enter cone height and radius >>>
6.8 2.75
The cone volume is 53.85
The cone surface area is 87.13
CONE: 6.80, 2.75
Enter pyramid base side length and height >>>
3.5 5.75
The pyramid volume is 23.48
The pyramid surface area is 54.32
PYRAMID: 3.50, 5.75
This code must work with a driver program located in the file
homework10.cpp.
This is what
your project should look like:
Once you have all of these files compiling and generating the correct output as shown below,
then you must zip the files together and submit the zip file to Web-CAT.
FORMATTING:
For this program, approximate
PI = 3.1415926535898
. Declare all your
numerical variables as doubles.
You
may
#include additional headers, depending on
how you set up your *.h files. You also
may
(or not)
need to have a using statement here.
Enter cone height and radius >>>
6.8 2.75
The cone volume is 53.85
The cone surface area is 87.13
CONE: 6.80, 2.75
Enter pyramid base side length and height >>>
3.5 5.75
The pyramid volume is 23.48
The pyramid surface area is 54.32
PYRAMID: 3.50, 5.75
This code must work with a driver program located in the file
homework10.cpp.
This is what
your project should look like:
Once you have all of these files compiling and generating the correct output as shown below,
then you must zip the files together and submit the zip file to Web-CAT.
FORMATTING:
For this program, approximate
PI = 3.1415926535898
. Declare all your
numerical variables as doubles.
You
may
#include additional headers, depending on
how you set up your *.h files. You also
may
(or not)
need to have a using statement here.


Development Suggestion:
1)
Start by commenting out the code in main() below line 22 shown above so that you
are only working on 1 small piece of the problem.
At first only work on the code for
Cylinder!
Figure out which line in main() is printing each line of output.
2)
Write the code (.h and .cpp) for the Cylinder class. You can Google the equations for
volume and surface area for all of the shapes.
3)
Get the code to compile.
4)
Get the code to work for the sample input and output shown above.
5)
Test your code for several different inputs and output (really small and really big), and
check your answers.
6)
Uncomment the next section of code in main() for the Sphere, write the next class’s code
and test it.
7)
Do the rest of the shapes.
Use private data members any time that you create a class.
Up to 20 points may be deducted for not using proper style.
You should declare PI as a global
constant.
Make certain that your program matches the sample output shown with the assignment
instructions.
As with all previous work, you can turn in late work for -10 points per day late, up to 3 days late.
One really important note…
The write( ) methods in each of the classes should look like this in the header files:
void
write(ostream& Out);
// in the header files
The parameter must be passed by reference!!! Copy this to your *.h files! You can see this
function in the skeletons provided. The write() function is also defined for you in the
cylinder.cpp file.
The answer
Description:
You will define five C++ classes—Cylinder, Sphere, Prism, Cone, and Pyramid—each with a constructor, volume()
, surface_area()
, and write(std::ostream&)
methods. Each class is implemented in separate .h
and .cpp
files. These classes encapsulate the data and formulas for each shape. They are designed to work with your main driver (homework6.cpp
or homework10.cpp
) for input/output and match the sample output format given in your assignment.
Explanation:
-
Each class contains private data members for its dimensions (all double
).
-
Each class’s constructor initializes its data.
-
The volume()
and surface_area()
methods compute and return the required values.
-
The write(ostream& Out)
method prints the object’s dimensions, matching the assignment output.
-
The global constant PI
is defined for use in all calculations.
-
All floating-point output uses fixed-point formatting with two decimals as shown in your sample.
Constants Header (Put in a header like constants.h
):
Cylinder Class
cylinder.h
cylinder.cpp
Sphere Class
sphere.h
sphere.cpp
Prism Class
(Rectangular Prism)
prism.h
prism.cpp
Cone Class
cone.h
cone.cpp
Pyramid Class
(Square Base)
pyramid.h
pyramid.cpp
Usage Example (in main or homework6.cpp)
Assuming you have:
All code follows good style, uses double throughout, and formats output as required.
📩 Need a similar solution? Email me: adel455@hotmail.com
