C++ – Automatic type conversion for user defined data types

Hello Friends,

Today In this article we will discuss about the /automatic Type Conversation in case of user defined types.

We have seen compiler does the type conversion in case of built in data types.
Sample code 1:

In the above case compiler tries to find a function named fun(), which takes an character type as argument, but don’t find any such parameter. So it calls the function fun(int), which is taking integer argument because character type can be type castes to integer type.

But to achieve similar effects in case of user defined data type or in custom data type, the programmer has to define automatic type conversion functions.

This can be done in 2 ways : 1> Constructor conversion 2> Operator conversion

Constructor Conversion

If a constructor is available which takes a single argument as an object or reference of another type , that constructor allows the compiler to perform automatic type conversion .

Sample code 2:

O/P:

default constructor of test_1

default constructor of test_2 made of test_1

inside function fun(test_2)

Here when compiler sees that, fun() is called with test_1 object, it looks in the declaration of fun() and notice it wants test_2 object. Then it check the possibility, to get test_2 object from test_1 object. And as a constructor is available (test_2(const test_1&))it can get test_2 object from test_1.

This features save us from using overloading functions (as seen in above example, without this concept, 2 version of fun() would be required , one with test_1 object and other with test_2 object ).

Sometimes this automatic type conversion via constructor creates problem. To prevent this, we should use explicit keyword before the constructor .
Same example with explicit keyword .

Operator Conversion

Using this feature, user can create a member function that takes the current type and converts it to the desired type using the operator keyword followed by the type you want to convert to. Here we don’t specify the return type. In this case the return type is the name of the operator user is overloading.

Sample Code 1:

Note* : Here we can see with the constructor conversion technique, the target type/destination class is performing conversion through the constructor with source type as argument. But in operator conversion, the source class is performing the conversion through operator overloading .

 

User has the choice, to follow any technique to achieve the Auto conversion feature. But in the scenario where user want to convert user defined type to built-in type, they must have to use operator conversion method as it is not possible to achieve with constructor conversion method .

One more example with Constructor type conversion :

Let’s consider User want to overload ‘+’ operator for a class test_class. so that below instructions should work fine .

  • test_class a,b;
  • cout<<a+b<<endl;
  • cout<<b+2<<endl;
  • cout<<2+b<<endl;

In this case, to make all the expressions to work, user has to use global overloaded function instead of member function because to call the member function left side of the operator must be a object which is not in case of the last expression (‘2+b’)
Without constructor conversion method, user has to define 3 different overloaded operator+ function to achieve this .      

  1. int operator + (test_class& a,test_class& b);
  2. int operator + (test_class& a,int data);
  3. int operator + (int data,test_class& a);

But using constructor conversion method , we can achieve the same with only one function .

Sample Code 2:

O/P :

inside test_class(int)

inside test_class(int)

30

inside test_class(int)

20

inside test_class(int)

20

As we can see , there is no exact functions to take (test_class, int) and (int , test_class) as argument. But as we explained earlier, the compiler will try to get the test_class object from int. As a constructor is available with int in test_class, the compiler will make the conversion and operator + function will be called.

Thanks for reading 🙂

Keep reading, share your thoughts, experiences. Feel free to contact us to discuss more. If you have any suggestion / feedback / doubt, you are most welcome.

Stay tuned on Knowledge-Junction, will come up with more such articles.

Advertisements

Comments

One comment on “C++ – Automatic type conversion for user defined data types”
  1. Manoj Sutar says:

    very good

Leave a Reply

This site uses Akismet to reduce spam. Learn how your comment data is processed.