A default constructor is a special constructor in C++ that is used for object initialization when no arguments are provided during creation. It ensures that objects are properly set up even without explicit values.
- It is automatically called when an object is created without passing parameters.
- It allows objects to be initialized with default values defined by the programmer or compiler.
Syntax
class ClassName {
public:
ClassName() {
// initialization code
}
};
Where:
- ClassName() is the default constructor.
- It has the same name as the class.
- It has no return type.
- It accepts no arguments.
Types of Default Constructor
A default constructor can be categorized into different types based on how it is defined and used in a C++ program.
Compiler-Generated Default Constructor
If a class does not define any constructor, the compiler automatically generates a default constructor.
- Created only when no constructor is declared.
- Performs default initialization of members.
- Suitable for simple classes.
class Student {
public:
int roll;
};
int main() {
Student s;
}
Explanation: Since no constructor is defined, the compiler automatically generates a default constructor, allowing the object s to be created.
User-Defined Default Constructor
A programmer can explicitly define a default constructor to perform custom initialization.
- Initializes data members.
- Executes custom code during object creation.
- Replaces the compiler-generated default constructor.
#include <iostream>
using namespace std;
class Student {
int roll;
public:
Student() {
roll = 101;
}
void display() {
cout << roll;
}
};
int main() {
Student s;
s.display();
}
Output
101
Explanation: The default constructor initializes roll with 101 whenever a Student object is created.
Initializing Objects Using Default Constructors
Objects can be initialized in different ways using a default constructor.
Member Initializer List
Instead of assigning values inside the constructor body, members can be initialized using a member initializer list.
- Initializes members before the constructor body executes.
- More efficient than assignment.
- Required for const members and references.
#include <iostream>
using namespace std;
class Student {
int roll;
public:
Student() : roll(101) {}
void display() {
cout << roll;
}
};
int main() {
Student s;
s.display();
}
Output
101
Default Constructor with Default Arguments
A constructor having all parameters with default values can also act as a default constructor.
#include <iostream>
using namespace std;
class Student {
int roll;
public:
Student(int r = 101) {
roll = r;
}
void display() {
cout << roll;
}
};
int main() {
Student s1;
Student s2(200);
s1.display();
cout << endl;
s2.display();
}
Output
101 200
Explanation: Since the constructor parameter has a default value, it can be called both with and without arguments.
Applications of Default Constructor
Default constructors are useful whenever objects need a predefined initial state.
- Initialize objects with default values.
- Create arrays of objects.
- Required by many STL containers.
- Used in templates and generic programming.
- Simplify object creation.
Related article: Explicitly Defaulted and Deleted Functions in C++ 11