top button
Flag Notify
    Connect to us
      Site Registration

Site Registration

What is conversion constructor? and what is Conversion Operator?

+1 vote
360 views
What is conversion constructor? and what is Conversion Operator?
posted Dec 6, 2014 by Alwaz

Share this question
Facebook Share Button Twitter Share Button LinkedIn Share Button

1 Answer

+1 vote

Conversion Constructor:

Constructor with a single argument makes that constructor as conversion ctor and it can be used for type conversion.

For example:
class Boo
{
public: Boo( int i );
};
Boo BooObject = 10 ; // assigning int 10 Boo object

Conversion operator:

Class can have a public method for specific data type conversions.

For example:

class Boo
{
double value;
public:
Boo(int i )
operator double()
{
return value;
}
};
Boo BooObject;
double i = BooObject;
// assigning object to variable i of type double.
now conversion operator gets called to assign the value.

answer Dec 10, 2014 by Mohammed Hussain
...