Quote:
Originally Posted by xpi0t0s View Post
The idea of a function is that you pass in values that you want the function to operate on. If length, width and depth are always going to be initialised to 10,20 and 30 then you don't need to pass anything into Box(). But if you want to set length to l etc, then you need to do length=l; instead of length=20.0;, and when you call Box, give it those values, e.g. Box(10,20,30).

I suggest you get the Rectangle class working first, then you can start work on Box. Take it a step at a time. "Rectangle::Rectangle(double = 1.0,double = 1.0);" - hmm, you seem to be taking "initialize: double=1.0 and double=1.0" rather too literally. The Rectangle constructor should take no parameters if you're always going to be using fixed values, or it should take two named parameters (e.g. Rectangle(double l,double w), then you can assign those values to length and width, i.e. length=1.0 if you're using fixed values, or length=l if you're using parameters.

In a way it's similar to maths; if a function is not dependent on specific variables then it's pointless passing them in. If f() is dependent on x but not y, then you would define f(x) rather than f(x,y); you could set f(x,y)=x^2, but then everyone would ask "why y?".

Correct results don't always mean the code is correct. Does the code give the correct results for different size boxes and rectangles?

You haven't specified the Rectangle::Rectangle function body anywhere, just a prototype. I'm not sure why the code works at all; Rectangle::length and Rectangle::width haven't been initialised anywhere. What compiler are you using?
I am using dev c++ 4.9.9.2..

So the
Rectangle::Rectangle(double=1.0,double=1.0)
should be more like
Rectangle::Rectangle(double l,double w)?

How would I initialize length and width to 1.0 using
Rectangle::length
Rectangle::width
since neither of these are functions?

sorry for so many questions as I am a beginner.

Let me repost the code so you can see the update and see if what I did was what you meant

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(double l,double w,double d)
     {
             length=l;
             width=w;
             depth=d;
     }
             double volume();
             double area();
              
};
 
double Box::volume()
{
       return(length * width * depth);
}
double Box::area()
{
       return(length * width);
}
int main()
{
    Rectangle r;
    Box b(3.0,4.0,5.0);
    
    cout<<r.area()<<endl;
    cout<<b.area()<<endl;
    cout<<b.volume()<<endl;
    
    system("pause");
    return 0;
}