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

  1. #1
    Registered User
    Join Date
    Sep 2002

    Total Surface Area of a Rectangle (Classes help)


    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. #2
    Guest Sebastiani's Avatar
    Join Date
    Aug 2001
    Waterloo, Texas
    Well I assume he/she means to derive a new object from a Rectangle. In that case you might do something like:

    class Rectangle3D : public Rectangle {
    double depth;
    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:

    double Rectangle3D::area()
       Rectangle s2(width, depth), 
                       s1(height, depth);
       return  (s1.area()*2) + (s2.area()*2) + (this->Rectangle::area()*2); 
    #include <cmath>
    #include <complex>
    bool euler_flip(bool value)
        return std::pow
            std::complex<float>(0, 1) 
            * std::complex<float>(std::atan(1.0)
            *(1 << (value + 2)))
        ).real() < 0;

  3. #3
    Registered User
    Join Date
    Sep 2002
    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. #4
    Registered User
    Join Date
    Mar 2002
    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;

    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;

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Need help with basic calculation program.
    By StateofMind in forum C Programming
    Replies: 18
    Last Post: 03-06-2009, 01:44 AM
  2. Help with day of the week program
    By Punkakitty in forum C++ Programming
    Replies: 10
    Last Post: 01-14-2009, 06:55 PM
  3. classes confusion
    By Trublu in forum C++ Programming
    Replies: 1
    Last Post: 06-07-2003, 01:19 PM
  4. Replies: 4
    Last Post: 04-22-2003, 12:52 PM
  5. Help With pointers in classes.
    By indigo0086 in forum C++ Programming
    Replies: 12
    Last Post: 10-10-2002, 02:03 PM