Conversion Constructor in C++

Conversion Constructor

We can use various arguments in many interesting ways, Conversion constructor is also very interesting as it allows us to call a constructor implicitly. That is the constructor can be called using 1 argument.

Constructor in C++

In other words we do not need to write className() to call the constructor.

  • This is done with the help of –
    • An assignment operator (uses overloading concept)
    • A parameterized constructor

Example 1

The example given below is the most basic type. The given parameterized constructor only allows one argument.

//case 1 

PrepInsta(int x)
{
//do something here
}

How Conversion Constructor will be called in Program

#include <iostream>
using namespace std;

class PrepInsta
{
int x, y;
public:
void display()
{
cout<<"The values are: " << x << " and " << y <<endl;
}

PrepInsta(int a)
{
x = a;
y = a;
}
};

int main()
{
//Basic parameterized constructor call below
PrepInsta p1(50);
p1.display();

//Conversion constructor invoked with single parameter and assignment operator
p1 = 100;
p1.display();

return 0;
}

Output:

The values are: 50 and 50
The values are: 100 and 100

Example 2 (Pseudo Parameters)

In below case we call the second parameter as pseudo parameter as its value is initialized in the definition itself.

//case 2

PrepInsta(int x, int y = 0)
{
  //do something here
}

How conversion constructor is called in this case

#include <iostream>
using namespace std;

class PrepInsta
{
int x, y;
public:
void display()
{
cout<<"The values are: " << x << " and " << y <<endl;
}

//parameterized constructor but argument two is initialized in definition itself
PrepInsta(int a, int b = 0)
{
x = a;
y = b; //or can write y = a also (output will change in this case)
}
};

int main()
{
//Basic parameterized constructor call below
//PrepInsta(int a, int b = 0) will be called by passing one parameter as below
//Since argument 2 initialization is in definition itself
PrepInsta p1(50);
p1.display();

//Conversion constructor invoked with single parameter and assignment operator
p1 = 100;
p1.display();

return
0;
}

Output –

The values are: 50 and 0 
The values are: 100 and 0
The values are: 50 and 50 
The values are: 100 and 100

Example 3 (Multiple Pseudo Parameters)

In below case we call both the parameter as pseudo parameter as their values is initialized in the definition itself.

//case 3 
PrepInsta(int x = 0, int y = 0)
{
//do something here
}

How Conversion constructor be called in this case

#include <iostream>
using namespace std;

class PrepInsta
{
int x, y;
public:
void display()
{
cout<<"The values are: " << x << " and " << y <<endl;
}

//parameterized constructor but both argument is initialized in definition itself
PrepInsta(int a = 0, int b = 0)
{
//value of a would be overloaded to 50 rather than 0
x = a;

//value of b will be 0 as nothing is passed in second argument,
//so initialized value in definition will be taken
y = b; //or can write y = a also (output will change in this case)
}
};

int main()
{
//Since, a value passed, is 50
//which is why 50 will be overloaded rather than a = 0 as initialized

PrepInsta p1(50);
p1.display();

//Conversion constructor invoked with single parameter and assignment operator
p1 = 100;
p1.display();
return 0;
}

Output –

The values are: 50 and 0 
The values are: 100 and 0
The values are: 50 and 50 
The values are: 100 and 100

Example 4 (Multiple Parameters)

In this case we can call the Conversion constructor using multiple arguments instead of 1 by doing follows –

p1 = {argument 1, argument 2}

Also both are pseudo parameters

PrepInsta(int a = 0, int b = 0)
{
  x = a;
  y = a; 
}

//call using multiple parameters
p1 = {100,200};
#include <iostream>
using namespace std;

class PrepInsta
{
  int x, y;
  public:
    void display()
    {
      cout<<"The values are: " << x << " and " << y <<endl;
    }

    //parameterized constructor but both arguments are initialized in definition itself
    PrepInsta(int a = 0, int b = 0)
    {
      x = a;
      y = a; //or can write y = b also (output will change in this case)
    }
};

int main()
{
  PrepInsta p1(50);
  p1.display();

  //Conversion constructor invoked with multiple parameters as list and assignment operator
   p1 = {100,200};
   p1.display();

  return 0;
}

Output –

The values are: 50 and 50 
The values are: 100 and 100

Example 5 (Multiple Parameters)

This is an interesting case and is self explanatory –

PrepInsta(int a = 0, int b = 0)
{
  x = a;
  y = a; 
}

//call using multiple parameters
p1 = {100,200};
#include <iostream>
using namespace std;

class PrepInsta
{
  int x, y;
  public:
    void display()
    {
      cout<<"The values are: " << x << " and " << y <<endl;
    }

    //parameterized constructor no arguments defined
    PrepInsta(int a, int b)
    {
      x = a;
      y = b; 
    }
};

int main()
{ 
//need to add second parameter here as constructor
//has no arguments initialized in definition PrepInsta p1(50,50); p1.display(); //Conversion constructor invoked with multiple parameters as list and assignment operator p1 = {100,200}; p1.display(); return 0; }

Output –

The values are: 50 and 50 
The values are: 100 and 200