Quote:
Originally Posted by xpi0t0s View Post
> When I change the code to what you suggested above it gives me compiler errors...

Yes, you will need to change the Box usage of Rectangle to reflect the constructor. The error occurs because Rectangle::Rectangle() doesn't exist (you've changed it to Rectangle::Rectangle(double,double)).

> I am feeling the way he is wanting us to do this problem is making things more difficult then they should be.

Depends. I don't know the context of the course, i.e. what you've done in the past. Maybe Rectangle was only ever meant to operate for 1x1 squares and we're now looking at class derivation. The Box class will work for boxes of any size so I don't understand why Rectangle is so limited. Possibly the point is that even though Rectangle only works for 1x1 squares due to there only being a parameterless constructor that initialises length and width to 1,1, this doesn't limit Box to 1x1xN cuboids.

> Would this be the correct formula to calculate the surface area of Box b?

Yes. For extra smartie points you could show you understand how to access overloaded functions from classes lower down the object hierarchy by changing that to
Code:
return((2*Rectangle::area())+(2*width * depth)+(2*length*depth));
and if he makes a comment about lower performance ask him about code profiling and what he thinks about premature optimisation <grin>
Thanks for the "smartie points" code help . I will see what he has to say about that.

I still am confused though on one thing. The 10.0, 20.0,30.0 isn't even going to be used at all in my program since Box b(3,4,5) has been declared in main, correct? Considering I am calling b.volume() and b.area(). So I don't understand why he even wants us to declare the 10,20,30 values. The Box b(3,4,5) will override those values everytime wont they?

Code:
#include<iostream>
using namespace std;
class Rectangle
{
      protected:
             double length;
             double width;
      public:
             Rectangle::Rectangle(double = 1.0,double =  1.0);
             double area();
             
};

Rectangle::Rectangle(double l,double w)
{
             length = l;
             width = w;
}
double Rectangle::area()
{
     return(length * width);
}

class Box : public Rectangle
{
    protected:
             double depth;
                
    public:
             Box::Box(double = 10.0,double = 20.0,double = 30.0);
             double volume();
             double area();
              
};
Box::Box(double l,double w, double d)
{
                length=l;
                width=w;
                depth=d;
                
}
                
 
double Box::volume()
{
       return(length * width * depth);
}
double Box::area()
{
       return((2*Rectangle::area())+(2*width * depth)+(2*length*depth));
}
int main()
{
    Rectangle r;
    Box b(3.0,4.0,5.0);
    
    cout<<"The area of the rectangle is: "<<r.area()<<endl;
    cout<<"\nThe volume of box b is: " <<b.volume()<<endl;
    cout<<"\nThe surface area of box b is: "<<b.area()<<endl;
    cout<<endl;
    
    system("pause");
    return 0;
}