A constructor without any arguments or with the default value for every argument is said to be the Default constructor.
A constructor that has zero parameter list or in other sense, a constructor that accept no arguments is called a zero argument constructor or default constructor.
If default constructor is not defined in the source code by the programmer, then the compiler defined the default constructor implicitly during compilation.
If the default constructor is defined explicitly in the program by the programmer, then the compiler will not defined the constructor implicitly, but it calls the constructor implicitly.
What is the significance of the default constructor?
They are used to create objects, which do not have any specific initial value.
Is a default constructor automatically provided?
If no constructors are explicitly declared in the class, a default constructor is provided automatically by the compiler.
Can a default constructor contain a default argument?
Yes, a constructor can contain default argument with default values for an object.
Will there be any code inserted by the compiler to the user implemented default constructor behind the scenes?
The compiler will implicitly declare the default constructor if not provided by the programmer, will define it when in need. The compiler-defined default constructor is required to do certain initialization of class internals. It will not touch the data members or plain old data types (aggregates like an array, structures, etc…). However, the compiler generates code for the default constructor based on the situation.
Consider a class derived from another class with the default constructor, or a class containing another class object with the default constructor. The compiler needs to insert code to call the default constructors of the base class/embedded object.
C++
#include <iostream>
using namespace std;
class Base {
public :
};
class A {
public :
A() { cout << "A Constructor" << endl; }
int size;
};
class B : public A {
};
class C : public A {
public :
C()
{
cout << "C Constructor" << endl;
}
};
class D {
public :
D()
{
cout << "D Constructor" << endl;
}
private :
A a;
};
int main()
{
Base base;
B b;
C c;
D d;
return 0;
}
|
Output
A Constructor
A Constructor
C Constructor
A Constructor
D Constructor
C++
Example:
#include<iostream>
using namespace std;
class student
{
int rno;
char name[50];
double fee;
public :
student()
{
cout<< "Enter the RollNo:" ;
cin>>rno;
cout<< "Enter the Name:" ;
cin>>name;
cout<< "Enter the Fee:" ;
cin>>fee;
}
void display()
{
cout<<endl<<rno<< "\t" <<name<< "\t" <<fee;
}
};
int main()
{
student s;
s.display();
return 0;
}
|
There are different scenarios in which the compiler needs to insert code to ensure some necessary initialization as per language requirements. We will have them in upcoming posts. Our objective is to be aware of C++ internals, not to use them incorrectly.
C++
#include <iostream>
using namespace std;
class A {
public :
int sum = 0;
A();
A( int a, int x = 0)
{
sum = a + x;
}
void print() { cout << "Sum =" << sum << endl; }
};
int main()
{
A obj1(10, 20);
A obj2(5);
obj1.print();
obj2.print();
return 0;
}
|
Explanation : Here, we have a constructor with two parameter- simple parameter and one default parameter. Now, there are two ways of calling this constructor:
- First, we can assign values to both the arguments and these values will be passed to the constructor and the default argument x with value 0 will be overridden by value passed while calling (in this case 20). Hence, code will give an output of 30 (as, sum= a+x i.e 10+20= 30).
- Second way is to not pass any value for the default parameter. If you do so, x will take it’s default value 0 as it’s final value and calculate a sum of 5 (as, sum = a+x i.e 5+0=5).
Please write comments if you find anything incorrect, or you want to share more information about the topic discussed above.
Whether you're preparing for your first job interview or aiming to upskill in this ever-evolving tech landscape,
GeeksforGeeks Courses are your key to success. We provide top-quality content at affordable prices, all geared towards accelerating your growth in a time-bound manner. Join the millions we've already empowered, and we're here to do the same for you. Don't miss out -
check it out now!
Last Updated :
16 Mar, 2023
Like Article
Save Article