Default Parameters

Why Default Parameters

You may have created a function in which you often are passing the same parameters. C++ has a nice feature that allows you to assign default parameters to a function.  The concept is simple:

#include <iostream>

using namespace std;

void TestingDefaults(int = 4, int = 2);  //prototype of function with default parameters

void TestingDefaults(int x, int y)          //function implementation declared normally
{
       cout << x << " " << y << endl;
}

void main(void)
{
       TestingDefaults();
       TestingDefaults(10);
       TestingDefaults(1,10);
}

In our prototype for the TestingDefaults function, we have defined that it will take two ints as parameters.  The ints are given the default values of 4 and 2 respectively.  

In main, the first call to TestingDefaults passes nothing.  The compiler resolves this by using the default values defined in the functions prototype.  x will be assigned a value of 4 ad y will be assigned a value of 2.

The second call to TestingDefaults passes only one parameter, the value 10.  The compiler uses 10 as the first parameter and the default value for the second parameter.  So x will be assigned a value of 10 and y will receive a value of 2.

In the final call to TestingDefaults in main, neither default value is used since two values are being passed in.  In this case x will receive 1 and y will receive 10.

One thing that the compiler would not be able to resolve is a call like this:

TestingDefaults( ,10);

You would think that the compiler would use the default value for the first parameter (because it is empty) and the passed in value for the second parameter.  It does not work that way; this is a syntax error.

Default Constructor

Default parameters are very useful in another area, class constructors.  One thing to keep in mind about constructors is that they are just like other functions except they do not have a return type.  This means constructors can do many of the things a regular function can do, such as have default parameters.  

Let's define a simple class called CNetwork, which we can pretend models a computer network.  We will not implement CNetwork yet, but we can look at its definition and see how default parameters work in the constructor: 

class CNetwork
{
public:
       //constructor
       CNetwork(int = 0, bool = false, float = 0.0f);   //constructor with default parameters

       //accessors
       int GetNumNodes() { return num_nodes; }
       bool GetSecure() { return is_secure; }
       float GetAvgTraffic() { return avg_traffic; }

       //modifiers
       void SetNumNodes(int);
       void SetSecure(bool);
       void SetAvgTraffic(float);

private:
       //members
       int num_nodes;
       bool is_secure;
       float avg_traffic;
};

It may be that all we want to do is create empty networks and later on add some stuff to them.  We could create two constructors to accomplish this: 

       //constructors

       CNetwork();                                          //empty constructor, we fill in the values
       CNetwork(int, bool, float);                   //constructor with parameters

This accomplishes the same thing as the example above, but it would require writing two overloaded implementations of the constructor.  Writing one constructor with default parameters takes care of both because the user can pass no parameters and the compiler will use the defaults, or the user can supply her own values.