C++ - Constructor with Default Arguments



A constructor is a special member function in a class, which is automatically called when an object is created. These are used to initialize the object with values or default settings.

Whereas default arguments in C++ allow to specify default values for function or constructor parameters.

Constructor with Default Arguments

A constructor with default arguments is a constructor that allows for the creation of an object using optional parameters. Where the default values for the parameters are provided, so when the user doesn't pass any values, the default value is used.

Syntax

Here is the syntax given for the constructor with default arguments:

class ClassName {
   public:
      ClassName(parameter_Type parameter_Name = default_Value,
              parameter_Type2 parameter_Name2 = default_Value2);
};
  • Here, the constructor has the same name as the class
  • Default values are provided for one or more parameters.
  • If no value is passed by the user, then this default value will be used.

Example of Constructor with Default Arguments

Here is the following example for constructor with default arguments:

#include <iostream>
using namespace std;

// Function with a default argument
void printMessage(string message = "Hello, Tutorialspoint Learner") {
   cout << message << endl;
}

int main() {
   // Calling the function without an argument
   printMessage();  // Prints the default message: "Hello, World!"
    
   // Calling the function with a custom argument
   printMessage("Hi, there!");  // Prints the custom message: "Hi, there!"

   return 0;
}

Output

Hello, Tutorialspoint Learner
Hi, there!

Explanation

  • A function printMessage() is created, giving a parameter of the default value "Hello, Tutorialspoint Learner".
  • Now in main(), printMessage() function is called without any argument, so the default argument "Hello, Tutorialspoint Learner" is used.
  • whereas in printMessage("Hi, there!"), we explicitly passed the value "Hi, there!" so this will override the default argument.

Constructor with Multiple Default Arguments

A constructor with multiple default arguments gives the user access to specify default values for more than one parameter. This provides more flexibility and access to pass any combination of arguments.

Example

Here is the following example for constructor with multiple default arguments:

#include <iostream>
using namespace std;

class Box {
   public:
      int length, width, height;

      // Constructor with multiple default arguments
      Box(int l = 5, int w = 10, int h = 2) {  // Default values for length,    width, and height
         length = l;
         width = w;
         height = h;
      }

      void display() {
         cout << "Length: " << length << ", Width: " << width << ", Height: " << height << endl;
      }
};

int main() {
   // Creating objects with different numbers of arguments
   Box box1;             
   Box box2(15);  
   Box box3(15, 20);     
   Box box4(15, 20, 25); 

   // Displaying the objects' values
   box1.display();
   box2.display();
   box3.display();
   box4.display();

   return 0;
}

Output

Length: 5, Width: 10, Height: 2
Length: 15, Width: 10, Height: 2
Length: 15, Width: 20, Height: 2
Length: 15, Width: 20, Height: 25

Explanation

  • Firstly, the class box is defined with three public member variables: length and width, where the constructor Box(int l = 5, int w = 10, int h = 2) has default arguments for all three parameters.
  • In object Box box1; no argument is provided, so the default constructor is called with default values, resulting in "Length: 5, Width: 10, Height: 2".
  • In Box box2(15); length is provided as 15, but width and height are not provided, So it uses the default values respectively.
  • In Box box3(15, 20); length and width are provided with values 15 and 20 respectively, so it will override the default values and result accordingly.

Key Features of Constructors with Default Arguments

While it is similar to regular functions, it provides more flexibility and convenience while creating objects.

Here in the following, we will discuss its all key features.

1. Default values for parameters and flexibility in object creation

Here the constructor can have default values for one or more parameters, which can be used when no argument is provided by the caller and allows multiple ways to create an object.

2. Avoiding multiple constructor overloads

You might need to load the constructor for every combination of arguments, which will make the code bulkier, but with default arguments, the constructor can be written once and it will handle different cases automatically.

3. Order of Default Arguments

In the case of multiple default value parameters, you cannot skip default arguments in the middle once you start providing defaults from the right.

Syntax

Box(int l = 1, int w);  // Invalid: 'w' has no default, but 'l' does.

4. Default Arguments Can Be Used with Const Members

If your class has const members, then default arguments can be provided in the constructor to make initialization easier.

Syntax

class Box {
   public:
      const int length, width;

      Box(int l = 5, int w = 10) : length(l), width(w) {}
};

This constructor uses default arguments (length = 5 and width = 10) to initialize the const members.

Advertisements