What is conversion constructor in C++?

In C++, if a class has a constructor which can be called with a single argument, then this constructor becomes conversion constructor because such a constructor allows automatic conversion to the class being constructed.

filter_none

edit
close

play_arrow

link
brightness_4
code

#include<iostream>
  
using namespace std;
class Test 
{
 private:
   int x;
 public:
   Test(int i) {x = i;}
   void show() { cout<<" x = "<<x<<endl; }
};
  
int main()
{
 Test t(20);
 t.show();
 t = 30; // conversion constructor is called here.
 t.show();
 getchar();
 return 0;
}

chevron_right


The above program prints:
x = 20
x = 30

Please write comments if you find anything incorrect, or you want to share more information about the topic discussed above



My Personal Notes arrow_drop_up


Article Tags :
Practice Tags :


5


Please write to us at contribute@geeksforgeeks.org to report any issue with the above content.