Site icon T4Tutorials.com

Types of constructors in C++

Types of constructors in C++

  1. Copy constructor
  2. Conversion constructor
  3. Default Constructor(also known as Zero argument constructor)
  4. Parameterized constructor
  5. Explicit constructor

Copy Constructor

A copy constructor is a like a normal parameterized Constructor, but in Copy Constructor, the parameter is the class object. In other words we can say that Copy constructor uses to initialize an object using another object of the similar class.

Syntax of Copy Constructor

class class_name {
public:
class_name(class_name & object) {
// object is the  another object of the same class
// Copy Constructor code
}

//… other Variables & Functions
}

When Copy Constructor is called?

Let’s see some scenarios where a copy constructor is called.

  1. When one object is initialized by the user with another existing object of the same class.
  2. When the object of the same class is passed by value as an argument.
  3. Whenever the object of the same class is generally returned by the function.

Types of Copy Constructor

There are two Types of Copy Constructor

C++ Program of Copy Constructor

#include <iostream>  
using namespace std;  
class T4Tutorials 
{  
   public:  
    int variable;  
    T4Tutorials(int a)               
	 // parameterized constructor.  
    {  
      variable=a;  
    }  
    T4Tutorials(T4Tutorials &i)               
	// copy constructor  
    {  
        variable = i.variable;  
    }  
};  
int main()  
{  
  T4Tutorials a1(20);               
  // Calling the parameterized constructor.  
 T4Tutorials a2(a1);                
 //  Calling the copy constructor.  
 cout<<a2.variable;  
  return 0;  
}

Conversion constructor in C++

Suppose that a class has a constructor which can be called with a single argument, then this constructor works as conversion constructor. The main reason to called it conversion constructor is  “because such a constructor allows automatic conversion to the class being constructed”.

Conversion constructor C++ Example

#include <iostream> 
using namespace std;  
class T4Tutorials { 
    int x, y; 
public: 
    T4Tutorials(int i) 
    { 
        x = i; 
        y = i; 
    } 
    void show() 
    { 
        std::cout << " x = " << x << " y = " << y <<endl; 
    } 
}; 
  
int main() 
{ 
    T4Tutorials object(10); 
    object.show(); 
    // Single parameter conversion constructor is invoked. 
    object = 20; 
    object.show(); 
    return 0; 
}

Default Constructor

Default constructor is the constructor which doesn’t take any parameter as a value.

Default Constructor C++ Example

// Cpp program to show Default Constructor 
#include <iostream> 
using namespace std; 
  
class T4Tutorials { 
public: 
    int x, y; 
  
    // Default Constructor 
    T4Tutorials() 
    { 
        x = 10; 
        y = 20; 
    } 
}; 
  
int main() 
{ 
    // Default constructor called  automatically 
    // when the object is created 
    T4Tutorials c; 
    cout << "x: " << c.x << endl 
         << "y: " << c.y; 
    return 1; 
}

Parameterized constructor

Parameterized constructor is a constructor with parameters as a values, it follows all the properties just like a normal constructor and takes parameters into the constructor to initialize the data and that is the reason to called it Parameterized constructor.

#include <iostream>
using namespace std;

class T4Tutorials
{
	private:
		int A;
		int B;
		int C;
	public:
		//parameterized constructor
		T4Tutorials(int A, int B, int C);
		void T4Tutorials_INPUT(int A, int B, int C);
		void T4Tutorials_SHOW();
};

T4Tutorials::T4Tutorials(int A, int B, int C)
{
	this->A  = A;
	this->B  = B;
	this->C  = C;
}

void T4Tutorials::T4Tutorials_INPUT(int A, int B, int C)
{
	this->A  = A;
	this->B  = B;
	this->C  = C;
}

void T4Tutorials::T4Tutorials_SHOW()
{
	cout<<"Value of A :  "<<A<<endl;
	cout<<"Value of B :  "<<B<<endl;
	cout<<"Value of C :  "<<C<<endl;
}

int main()
{
	//Parameterized Constructor called when object created
	T4Tutorials obj = T4Tutorials(5,7,2); 
	//here, 1,1,1 will be assigned to A,B and C
	//T4Tutorials_SHOWing the value
	obj.T4Tutorials_SHOW();
	//changing the  value using T4Tutorials_INPUT function
	obj.T4Tutorials_INPUT(5,9,100);
	//T4Tutorials_SHOWing the values
	obj.T4Tutorials_SHOW();

	return 0;
}

Explicit constructor

As we know that Constructors are functions with the same name as the class name. Constructors are used to create the instance of a class. If we don’t make any Constructors  then They are provided by default by the compiler itself. However, the user can also declare and use its own constructor by explicitly declaring the Constructors.

Exit mobile version