top button
Flag Notify
    Connect to us
      Facebook Login
      Site Registration Why to Join

Facebook Login
Site Registration

What is conversion constructor in C++?

+2 votes
204 views
What is conversion constructor in C++?
posted Mar 28, 2014 by Simranjeet Singh

Share this question
Facebook Share Button Twitter Share Button Google+ Share Button LinkedIn Share Button Multiple Social Share Button

2 Answers

+1 vote

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.

Example

class Test 
{
 private:
   int x;
 public:
   Test(int i) {x = i;}
   void show() { cout<<" x = "<<x<<endl; }
};

int main()
{
 Test t(200);
 t.show();
 t = 300; // conversion constructor is called here.
 t.show();
}

The above program prints:

x = 200
x = 300
answer Mar 28, 2014 by Luv Kumar
0 votes

Constructor which is called with single argument used for converting from the type of the argument to the class type is called conversion constructor.

answer Apr 2, 2014 by Karan Joglekar
Contact Us
+91 9880187415
sales@queryhome.net
support@queryhome.net
#280, 3rd floor, 5th Main
6th Sector, HSR Layout
Bangalore-560102
Karnataka INDIA.
QUERY HOME
...