Converting Default Arguments

One extensively used feature of c++ that Java docs not support is default  unction arguments. For example, tile areal ) function shown in the following c++ program computes the area of a rectangle if called with two arguments, 01′ the area of a square if called with one argument.

As Connolly see, when are is called with ‘only one argument, the second default to zero. When this happens, the {unction simply uses the first argument for both the
length and the width of the rectangle. ‘ While convenient, default arguments are not, of course, necessary, In essence, default arguments are actually n shorthand form of (unction over London in which  one form of the function has a different number of parameters than the other.  to convert a c++ function that contains one or more default arguments into Java, simply center overloaded methods that handle each case, In this example, you need n version of  that takes two arguments and another that takes only one argument.  approach, here Is the preceding program rewritten for Java:

[vfb id=1]

Share This