
- C++ Home
- C++ Overview
- C++ Environment Setup
- C++ Basic Syntax
- C++ Comments
- C++ Hello World
- C++ Omitting Namespace
- C++ Tokens
- C++ Constants/Literals
- C++ Keywords
- C++ Identifiers
- C++ Data Types
- C++ Numeric Data Types
- C++ Character Data Type
- C++ Boolean Data Type
- C++ Variable Types
- C++ Variable Scope
- C++ Multiple Variables
- C++ Basic Input/Output
- C++ Modifier Types
- C++ Storage Classes
- C++ Numbers
- C++ Enumeration
- C++ Enum Class
- C++ References
- C++ Date & Time
- C++ Operators
- C++ Arithmetic Operators
- C++ Relational Operators
- C++ Logical Operators
- C++ Bitwise Operators
- C++ Assignment Operators
- C++ sizeof Operator
- C++ Conditional Operator
- C++ Comma Operator
- C++ Member Operators
- C++ Casting Operators
- C++ Pointer Operators
- C++ Operators Precedence
- C++ Unary Operators
- C++ Control Statements
- C++ Decision Making
- C++ if Statement
- C++ if else Statement
- C++ Nested if Statements
- C++ switch Statement
- C++ Nested switch Statements
- C++ Loop Types
- C++ while Loop
- C++ for Loop
- C++ do while Loop
- C++ Foreach Loop
- C++ Nested Loops
- C++ break Statement
- C++ continue Statement
- C++ goto Statement
- C++ Strings
- C++ Strings
- C++ Loop Through a String
- C++ String Length
- C++ String Concatenation
- C++ String Comparison
- C++ Functions
- C++ Functions
- C++ Multiple Function Parameters
- C++ Recursive Function
- C++ Return Values
- C++ Function Overloading
- C++ Function Overriding
- C++ Default Arguments
- C++ Arrays
- C++ Arrays
- C++ Multidimensional Arrays
- C++ Pointer to an Array
- C++ Passing Arrays to Functions
- C++ Return Array from Functions
- C++ Structure & Union
- C++ Structures
- C++ Unions
- C++ Pointers
- C++ Pointers
- C++ Dereferencing
- C++ Modify Pointers
- C++ Class and Objects
- C++ Object Oriented
- C++ Classes & Objects
- C++ Class Member Functions
- C++ Class Access Modifiers
- C++ Static Class Members
- C++ Static Data Members
- C++ Static Member Function
- C++ Inline Functions
- C++ this Pointer
- C++ Friend Functions
- C++ Pointer to Classes
- C++ Constructors
- C++ Constructor & Destructor
- C++ Default Constructors
- C++ Parameterized Constructors
- C++ Copy Constructor
- C++ Constructor Overloading
- C++ Constructor with Default Arguments
- C++ Delegating Constructors
- C++ Constructor Initialization List
- C++ Dynamic Initialization Using Constructors
- C++ Inheritance
- C++ Inheritance
- C++ Multiple Inheritance
- C++ Multilevel Inheritance
- C++ Object-oriented
- C++ Overloading
- C++ Polymorphism
- C++ Abstraction
- C++ Encapsulation
- C++ Interfaces
- C++ Virtual Function
- C++ Pure Virtual Functions & Abstract Classes
- C++ File Handling
- C++ Files and Streams
- C++ Reading From File
- C++ Advanced
- C++ Exception Handling
- C++ Dynamic Memory
- C++ Namespaces
- C++ Templates
- C++ Preprocessor
- C++ Signal Handling
- C++ Multithreading
- C++ Web Programming
- C++ Socket Programming
- C++ Concurrency
- C++ Advanced Concepts
- C++ Lambda Expression
- C++ unordered_multiset
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.