# Thread: Total Surface Area of a Rectangle (Classes help)

1. ## Total Surface Area of a Rectangle (Classes help)

Hello,

I am currently enrolled in a c++ class in my college and this week we have a difficult assignment (for me anyway)

Anyway here is the assignment:

Assume the length, width, and height of a rectangular box are 3, 4, and 5 inches respectively. Write a C++ program that uses the Rectangle class (in the rect.h header file) to define each side of the box as an object to compute the total surface area of all six sides of the box.
I understand how to get total surface area (2*(l*w), 2*(h*w), 2*(h*l), the sum of those) however I'm confused about how to implement this. I have to use the rectangle class (rect.h file, attached)

Within the class file only length and width are declared. What can I do to declare height? I have to declare all six sides of the rectangle as an object.

If I were to make an edit to the rect.h file such as add height property wouldnt that throw everything off?

Thank you

2. Well I assume he/she means to derive a new object from a Rectangle. In that case you might do something like:

Code:
```class Rectangle3D : public Rectangle {

double depth;

public:

Rectangle3D(double l, double w, double d)
: Rectangle(l, w), depth(d)
{}

void setSides( double l, double w, double h)
{
Rectangle::setSides(l, w);
depth = d;
}

};```
For the area function, just keep in mind that there are really only 3 sides to consider, since each is mirrored by an identical rectangle.

Here's an example of using the existing class:

Code:
```double Rectangle3D::area()
{
Rectangle s2(width, depth),
s1(height, depth);

return  (s1.area()*2) + (s2.area()*2) + (this->Rectangle::area()*2);
}```

3. Thank you very much for your assistance... I'm starting to piece the puzzles together and the example you presented was very very helpful

4. you don't have to derive a new class from rectangle (if you don't want to) since each side of the box is a rectangle in and of itself. Therefore each side has a length and a width. Height (or whichever is the third dimension in your mind) is a concept that only needs to be taken into account when you think of the box as three dimensional.

assume box has length = 5, width = 4; height = 3;
front side and back side of box have length and height
left side and right side of box have width and height
top side and bottom side of box have length and width

a rectangle has l and w
all sides of the box are rectangles

front side = back side has l = length = 5; w = height = 3
left side = right side has l = height = 3; w = width = 4;
top side = bottom side has l = length = 5; w = width = 4;

now in your code declare three instances of rectangle, one for each set of sides, passing in the dimensions of l and w for each instance;

Code:
```int main()
{
Rectangle rectangle1(5, 3);
Rectangle rectangle2(3, 4);
Rectangle rectangle3(5, 4);

int surfaceAreaOfBox = 2(rectangle1.l * rectangle1.w) + 2(rectangle2.l * rectangle2.w) + 2(rectangle3.l * rectangle3.w);

cout << surfaceAreaOfBox;
return 0;
}```