How many types of constructors in C++?
Loading
How many types of constructors in C++?
Know the answer? Post it — somebody with the same question will find it here.
Sign in to answer this question
It is the same account you read, post and publish with — and you will come straight back to this page.
Amit MohantyPosted Oct 12, 2022, 7:55 AM
There are 3 types of Constructors.
1. Default Constructor
A constructor with no arguments (or parameters) in the definition is a default constructor. Usually, these constructors use to initialize data members (variables) with real values.
Note: If no constructor is explicitly declared, the compiler automatically creates a default constructor with no data member (variables) or initialization.
Syntax of Default Constructor –
class CLASS_NAME
{
………
public :
CLASS_NAME() //Default constructor
{
. . . . . .
}
//other member functions
};
Example:
2. Parameterized Constructor
Unlike, Default constructor, It contains parameters (or arguments) in the constructor definition and declaration. More than one argument can also pass through a parameterized constructor.
Moreover, these types of constructors use for overloading to differentiate between multiple constructors.
Syntax –
class class_Name
{
public:
class_Name(datatype variable) //Parameterized constructor
{
………………..
}
};
Example:
3. Copy Constructor
A copy constructor is a member function that initializes an object using another object of the same class. It helps to copy data from one object to another.
Example –