2

I watched my lecturer's video from my university and he says about the Rational class that its constructor goes like this:

Rational (int top=0 , int bottom=1)
: t(top) , b(bottom) {normalize();}

Until now everything is OK, BUT !! he also said that you can call the constructor only with 1 argument (top argument) and because that bottom initialize to value of 1 the rational for example: Rational(3) will be 3/1.

BUT !! I wonder how can we use a constructor with 1 value only if it supports 2 arguments ONLY ?

I know that in java if we have x number of argument that the constructor receive (consider no other constructor and x>0) we must transfer them all not 1 instead of 2...

Please help me solve this conflict...

thnx...

Rob Kennedy
  • 161,384
  • 21
  • 275
  • 467
Master C
  • 1,536
  • 4
  • 14
  • 19

4 Answers4

10

The = in the constructor declaration give the parameters default values. If you don't provide a value yourself when you call the constructor, the compiler will fill in the declared default value for you. The constructor won't know the difference — it will see both parameters, and it will have no way of detecting whether the caller provided both values or whether the compiler filled in some of them, but that's usually OK. (If you need to know the difference, then declare multiple constructors, each with a distinct number of parameters, and no default values.)

Your constructor can even be called with no parameters because both of them have default values. In that case, the first parameter will have the default value 0 and the second will get 1.

Parameter values can only be omitted from the end. That is, you cannot omit the top parameter without also leaving out the bottom parameter. The first actual parameter you give will always correspond to the first formal parameter in the declaration. Likewise, default parameters can only be defined starting at the end. You cannot define a default parameter for top without also declaring one for bottom.

Rob Kennedy
  • 161,384
  • 21
  • 275
  • 467
2

When you do this:

Rational r(42);

...the default value of 1 is supplied to the parameter bottom, because your constructor has default values for the bottom parameter. (That's that the =1 is about)

If you were to change the declaration of th constructor to not include any defaults:

Rational(int top, int bottom)

...then you would no longer be able to construct a Rational object without specifying both parameters explicitly.

John Dibling
  • 99,718
  • 31
  • 186
  • 324
2

To extend Rob Kennedy's answer, here are a few examples of what does and does not work:

Imagine a class Foo:

class Foo
{
    Foo( int a = 0, float b = 1.0f );
}

And consider the following constructor-calls:

Foo foo_obj = Foo(5, 6.0f);    // Fine, two arguments are passed. a will be 5 and b will be 6.0f.

Foo foo_obj = Foo(5);          // Fine as well. a will be 5 and b will be 1.0f. This is because b has a default value in the constructor.

Foo foo_obj = Foo();           // Fine too, a will be 0 and b will be 1.0f. This is because both a and b have default values in the constructor.

Mind you that variables are still passed left-to-right. By this I mean that you cannot leave out any argument that comes before the argument that you want to explicitly pass. By this I mean that in the above example, you can not pass a value for b, but leave out a value for a.

Rycul
  • 366
  • 1
  • 8
0

top and bottom are arguments with implicit values. That means you can skip them when calling the function and their implicit values will be used for calling the function.

So with the given constructor you can say Rational r; and r will have top 0 and bottom 1, or Rational r(42), in witch case top will be 42 and bottom 1, or Rational r(1,2); and top be 1 and bottom 2.

Marius Bancila
  • 16,053
  • 9
  • 49
  • 91