Write a class called Box that has three double fields called height, width and l
ID: 3664632 • Letter: W
Question
Write a class called Box that has three double fields called height, width and length. The class should have set methods for each field. It should have a three-parameter constructor that takes three doubles and passes them to the set methods to initialize the fields of the Box. It should have a default constructor that uses the set methods to initialize each field to 1. It should have a method that calculates and returns the volume of the Box and a method that calculates and returns the surface area of the Box. The class declaration (interface) and the function definitions (implementation) must be in separate files - the interface or "header" file has a .hpp extension and the implementation has a .cpp extension. All data members should be private.
Your functions should have the following names:
setHeight
setWidth
setLength
getVolume
getSurfaceArea
The files must be named: Box.hpp and Box.cpp
About using multiple files:
-Box.hpp should have "include guards" as discussed on page 447 (use "BOX_HPP").
-Box.cpp needs to #include Box.hpp. When you include your own .hpp files (header files), put double quotes around them instead of angled brackets. (You should only #include .hpp files, not .cpp files.)
-When testing your program with your own main method, put it in a separate file (this is the "client" code) and give it a name with a .cpp extension.
-Your main method also needs to #include Box.hpp.
-If you named the file with your main method "boxMain.cpp", then you can compile your program with "g++ Box.cpp boxMain.cpp -o box".
Explanation / Answer
As per given problem Box class is implemented with setter and getSurfaceArea and getVolume methods
To calculate the SurfaceArea and Volume of given Box Parameters.
See the below Code..
#include <iostream>
using namespace std;
// Box Class
class Box
{
// Box Class Data Members
private:
double Length;
double Height;
double Width;
public:
// Parametrised Box Constructor
Box(double Length1, double Width1,double Height1)
{
Length = Length1;
Width = Width1;
Height = Height1;
}
// Default Constructor
Box()
{
Length = 0.0;
Width = 0.0;
Height = 0.0;
}
// Setter for Height, Length and Width
void setLength(double Length1)
{
Length = Length1;
}
void setHeight(double Height1)
{
Height = Height1;
}
void setWidth(double Width1)
{
Width = Width1;
}
// Volume Method
double getVolume()
{
return (Length * Width * Height);
}
// SurfaceArea Method
double getSurfaceArea()
{
return (2.0 * ((Height * Width) +
(Length * Height) +
(Length * Width)));
}
};
int main()
{
// Calling Box Object
Box box1(8,4,5); //Initializing box1 data members
// Volume and surfaceArea value printing
double boxVolume = box1.getVolume();
double surfaceArea = box1.getSurfaceArea();
cout << "Volume of Box = " << boxVolume <<" SurfaceArea of Box is = "<<surfaceArea<< endl;
return 1;
}
Output:
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.