What is converting constructor in C++ ?

C++Server Side ProgrammingProgramming

In this section we will see what is the conversion constructor or converting constructor in C++ class. A constructor is a special type of function of class. It has some unique property like, its name will be same as class name, it will not return any value etc. The constructors are used to construct objects of a class. Sometimes constructors may take some arguments, or sometimes it does not take arguments.

When a constructor takes only one argument then this type of constructors becomes conversion constructor. This type of constructor allows automatic conversion to the class being constructed.


using namespace std;
class my_class{
      int my_var;
      my_class(int x){
         this->my_var = x; //set the value of my_var using
         parameterized constructor
      void display(){
      cout << "The value of my_var is: " << my_var <<endl;
int main() {
   my_class my_obj(10);
   my_obj = 50; //here the conversion constructor is called


The value of my_var is: 10
The value of my_var is: 50
Published on 30-Apr-2019 15:15:14