Cpp Tutorial: Different types of Constructors in C++

In this tutorial we shall learn about below different types of Constructors:
1. Default Constructor
2. Parameterized constructors
3. Overloading constructors
4. Constructor with default arguments

1. Default constructor:

1. A constructor that does not take any arguments is called as default constructor.
2. When an object is created default constructor is called.
3. Default constructor cannot be overloaded, as it takes no parameters.
4. If there is no default constructor defined in the class, the compiler will crete one.
5. Default constructors are useful when we want to initialize all the data member with same values when objects are created.
6. Constructors should be always public.
Example:
#include<iostream>
using namespace std;

class Ex
{
	public:
	Ex()
	{
		cout<<"Default Constructor is called"<<endl;
	}
};

int main()
{
Ex e;
return 0;
}
Output:
Default Constructor is called

2. Parameterized constructors

As we discussed earlier, we can overload a constructor. Hence we can create constructor that accepts parameters. Hence while creating objects we should pass the parameters to those objects.
Parameterized constructors:
1. Constructors that accepts parameter are called as parameterized constructors.
2. They can be overloaded.
3. They are used when we need to initialize different objects with different values.
4. They can have default values.
5. If we define parameterized constructor, default constructor will not be created by compiler.
Parameterized constructor can be called by 2 types.
1. Implicit call
2. Explicit Call
Example:
#include<iostream>
using namespace std;

class Ex
{
	private:
		int a;
		int b;
	public:
	
	Ex(int x, int y)
	{
		a = x;
		b = y;
		cout<<"Parameterized Constructor is called"<<endl;
	}
	
	void display()
	{
	  cout<<"The value of a = "<<a<<" The value of b = "<<b<<endl;
	}

};

int main()
{
Ex e(10, 20); //Implicit call
e.display();

Ex e1 = Ex(20, 30); //Explicit call
e1.display();

return 0;
}
Output:
Parameterized Constructor is called
The value of a = 10 The value of b = 20
Parameterized Constructor is called
The value of a = 20 The value of b = 30

3. Overloading constructors

Like functions constructors can also be overloaded. Below is an example for the same.
Example for Overloading constructors
/*
* File     : constructor_overloading.cpp
* Copyright: @ prodevelopertutorial.com
*/
#include<iostream>

using namespace std;

class Example
{

	int num;
public:
	Example();
	Example(int a);
	void display();
};

Example :: Example()
{
	cout<<"Default Constructor"<<endl;
}

Example :: Example(int a)
{
	num = a;
	cout<<"Parameterized Constructor num value is = "<<num<<endl;

}

int main()
{
	Example e1;

	Example e2(10);

	return 0;
}
Output:
Default Constructor
Parameterized Constructor num value is = 10

4. Constructor with default arguments.

Like function, constructors can also have default arguments. Same rules apply as function default arguments.
Example for constructors with default arguments
/*
* File     : constructor_Default_arguments.cpp
* Copyright: @ prodevelopertutorial.com
*/
#include<iostream>
using namespace std;

class Example
{
	int num_1;
	int num_2;
public:
	Example(int a, int b = 30);
	void display();
};

Example :: Example(int a, int b)
{
	num_1 = a;
	num_2 = b;
}


void Example :: display()
{
	cout<<"num_1 value is = "<<num_1 <<" and num_2 is = " << num_2<<endl;
}

int main()
{
	Example e2(10);
	e2.display();
	return 0;
}

Output:

num_1 value is = 10 and num_2 is = 30
Write a Comment

Leave a Comment

Your email address will not be published. Required fields are marked *