Academic Integrity: tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.

(C++) Write a class called Box that has three double fields called height, width

ID: 3664556 • Letter: #

Question

(C++) 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

Explanation / Answer

Box.hpp:

class Box
{
private:
double height, width, length;
public:
Box(double h, double w, double l);
Box();
void setHeight(double h);
void setWidth(double w);
void setLength(double l);
double getVolume();
double getSurfaceArea();
};

Box.cpp:

#include "Box.hpp"
Box::Box()
{
setHeight(1);
setWidth(1);
setLength(1);
}
Box::Box(double h, double w, double l)
{
setHeight(h);
setWidth(w);
setLength(l);
}
void Box::setHeight(double h)
{
height = h;
}
void Box::setWidth(double w)
{
width = w;
}
void Box::setLength(double l)
{
length = l;
}
double Box::getVolume()
{
return length * width * height;
}
double Box::getSurfaceArea()
{
return 2*width*length + 2*width*height + 2*height*length;
}
  
  As you didn't asked the main() function implementation, I didn't wrote that. If you need any refinements, just get back to me.