C++ Classes for Shape Volume and Surface Area Calculations

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

C++ Classes for Shape Volume and Surface Area Calculations


Background image of page 1

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.
Background image of page 2
Homework 6 ▬ 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: 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 h ere. 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. 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.
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):

cpp

#ifndef CONSTANTS_H #define CONSTANTS_H const double PI = 3.1415926535898; #endif

Cylinder Class

cylinder.h

cpp

#ifndef CYLINDER_H #define CYLINDER_H #include <iostream> #include "constants.h" class Cylinder { private: double height; double radius; public: Cylinder(double h, double r); double volume(); double surface_area(); void write(std::ostream& Out); }; #endif

cylinder.cpp

cpp

#include "cylinder.h" #include <iomanip> Cylinder::Cylinder(double h, double r) : height(h), radius(r) {} double Cylinder::volume() { return PI * radius * radius * height; } double Cylinder::surface_area() { return 2 * PI * radius * (radius + height); } void Cylinder::write(std::ostream& Out) { Out << "CYLINDER: " << std::fixed << std::setprecision(2) << height << ", " << radius << std::endl; }

Sphere Class

sphere.h

cpp

#ifndef SPHERE_H #define SPHERE_H #include <iostream> #include "constants.h" class Sphere { private: double radius; public: Sphere(double r); double volume(); double surface_area(); void write(std::ostream& Out); }; #endif

sphere.cpp

cpp

#include "sphere.h" #include <iomanip> Sphere::Sphere(double r) : radius(r) {} double Sphere::volume() { return (4.0 / 3.0) * PI * radius * radius * radius; } double Sphere::surface_area() { return 4 * PI * radius * radius; } void Sphere::write(std::ostream& Out) { Out << "SPHERE: " << std::fixed << std::setprecision(2) << radius << std::endl; }

Prism Class

(Rectangular Prism)

prism.h

cpp

#ifndef PRISM_H #define PRISM_H #include <iostream> #include "constants.h" class Prism { private: double base_length; double height; double width; public: Prism(double bl, double h, double w); double volume(); double surface_area(); void write(std::ostream& Out); }; #endif

prism.cpp

cpp

#include "prism.h" #include <iomanip> Prism::Prism(double bl, double h, double w) : base_length(bl), height(h), width(w) {} double Prism::volume() { return base_length * height * width; } double Prism::surface_area() { return 2 * (base_length * width + base_length * height + height * width); } void Prism::write(std::ostream& Out) { Out << "PRISM: " << std::fixed << std::setprecision(2) << base_length << ", " << height << ", " << width << std::endl; }

Cone Class

cone.h

cpp

#ifndef CONE_H #define CONE_H #include <iostream> #include "constants.h" class Cone { private: double height; double radius; public: Cone(double h, double r); double volume(); double surface_area(); void write(std::ostream& Out); }; #endif

cone.cpp

cpp

#include "cone.h" #include <iomanip> #include <cmath> Cone::Cone(double h, double r) : height(h), radius(r) {} double Cone::volume() { return (PI * radius * radius * height) / 3.0; } double Cone::surface_area() { double slant_height = std::sqrt(height * height + radius * radius); return PI * radius * (radius + slant_height); } void Cone::write(std::ostream& Out) { Out << "CONE: " << std::fixed << std::setprecision(2) << height << ", " << radius << std::endl; }

Pyramid Class

(Square Base)

pyramid.h

cpp

#ifndef PYRAMID_H #define PYRAMID_H #include <iostream> #include "constants.h" class Pyramid { private: double base_side; double height; public: Pyramid(double bs, double h); double volume(); double surface_area(); void write(std::ostream& Out); }; #endif

pyramid.cpp

cpp

#include "pyramid.h" #include <iomanip> #include <cmath> Pyramid::Pyramid(double bs, double h) : base_side(bs), height(h) {} double Pyramid::volume() { return (base_side * base_side * height) / 3.0; } double Pyramid::surface_area() { double base_area = base_side * base_side; double slant_height = std::sqrt((base_side/2)*(base_side/2) + height*height); double lateral_area = 2 * base_side * slant_height; return base_area + lateral_area; } void Pyramid::write(std::ostream& Out) { Out << "PYRAMID: " << std::fixed << std::setprecision(2) << base_side << ", " << height << std::endl; }

Usage Example (in main or homework6.cpp)

Assuming you have:

cpp

#include "cylinder.h" #include "sphere.h" #include "prism.h" #include "cone.h" #include "pyramid.h" // ... input code as per your assignment Cylinder cyl(h, r); std::cout << "The cylinder volume is " << std::fixed << std::setprecision(2) << cyl.volume() << std::endl; std::cout << "The cylinder surface area is " << std::fixed << std::setprecision(2) << cyl.surface_area() << std::endl; cyl.write(std::cout); // ... similar for other shapes

All code follows good style, uses double throughout, and formats output as required.



📩 Need a similar solution? Email me: adel455@hotmail.com












Background image of page 3
Previous Post Next Post

Comments

Nepali Graphics - Learn design, Animation, and Progrmming