Quote:
Originally Posted by xpi0t0s View Post
Here's what I think it should be:
Code:
class Rectangle
{
      protected:
             double length;
             double width;
      public:
             Rectangle::Rectangle(double l,double w);
             double area();
 
};
 
Rectangle::Rectangle(double l,double w)
{
             length = l;
             width = w;
}
 
and in main:
 
Rectangle r(1.0, 1.0);
However the ambiguous task specification has me baffled. What have the values 10, 20 and 30 got to do with anything? Are they meant to be default values? (which would mean Box r; would be equivalent to Box r(10,20,30); ) Similarly what have the two 1.0 values got to do with anything? Are they defaults, or are we (maybe for the sake of learning) defining a Rectangle object that will only ever compute the area of 1x1 squares, which will always be just 1? It would seem to make more sense to me for Rectangle to be generic, so you could define Rectangle r1, , etc.
The 10,20,30 are just values that my professor threw into the problem for default values. I definately see where you are coming from and would have to agree that it would make more sense to just do r2(10,2), r3(100,50) etc. Again though the problem states
" In function main(), declare an object of Rectangle class r and declare an object of Box class called b(3,4,5). Then display r.area(), b.area(), and b.volume()."
so this means that he is expecting just two objects to be declared.
Rectangle r;
Box b(3,4,5)

When I change the code to what you suggested above it gives me compiler errors similar to the ones I posted in my first/second post.

In constructor `Box::Box(double)':
no matching function for call to `Rectangle::Rectangle()'
candidates are: Rectangle::Rectangle(const Rectangle&)
note C:\Users\Chris\Documents\C++\cemHW13.cpp:11 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.