Industrial manufacturing
Industrial Internet of Things | Industrial materials | Equipment Maintenance and Repair | Industrial programming |
home  MfgRobots >> Industrial manufacturing >  >> Industrial programming >> C Language

C++ Constructors: Using Default Arguments for Flexible Object Creation

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);
};

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

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

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.


C Language

  1. Mastering Character Pointers and Function Usage in C
  2. Understanding C# Nested Classes: Definition, Usage, and Inheritance
  3. Mastering C# Collections: Stacks, Queues, Lists & Hash Tables
  4. Mastering C Input and Output (I/O): scanf() and printf() Explained
  5. Mastering C++ Overloading: Functions & Operators Explained
  6. Master C++ Web Programming with CGI
  7. Master C Increment and Decrement Operators: Usage & Best Practices
  8. 24 Essential C++ Interview Questions & Expert Answers (2021 Update)
  9. Understanding C# Interfaces: Definition, Example, and Practical Use
  10. Mastering C Enums: Definition, Declaration, and Flag Operations