Conversion Constructor in C++ With Example

Conversion Constructor
Conversion Constructor

What is Conversion Constructor?

In this Article, we will explore how do we use Conversion Constructor. We can say a constructor as a converter when the constructor is having a single argument. To dig more into this, look at the below picture:

Conversion Constructor
Conversion Constructor

The Constructor for Train accepts Boat as a parameter. Note that this Constructor has only one argument and hence it can act as a converter. The conversion is from Boat to Train. In this article, we will see an example that uses the Conversion Constructor. Now, we will code our example.

2. Private Members

DivBy5 class has two members. The member variables divided and remaining will hold the result of the division by five. Below are the members:

3. Member Functions

We have two member functions in the Class DivBy5 . The GetReminder function returns the member remaining  and GetDivided function return the Class member  divided . Note, these two functions are in public scope. Below is the code:

4. Conversion Constructor Implementation

4.1 Constructor as Converter

As Previously said, a class with a single parameter can be used as a Conversion Constructor. The Constructor shown below has a single integer as a parameter and constructs the class object of type DivBy5. When we do not give anything to the Constructor, the member has the value zero. When we pass integer number, the divided member stores the dividend value and remaining is stored in the other member of the class object.

We apply the division operator to the supplied number to get the divided value. And we use the Modulus (%) to hold the remainder. We use these two operators division (/) and the Modulus (%) when we have a valid number supplied to the constructor. Below is the code:

4.2 Conversion Constructor: Usage 1

In the First Code Snippet (//Sample 05), we used the constructor in a normal way. That is; we create the object by passing the integer value to it like DivBy5 Obj(22) . Code is below:

 

The Picture below shows the result of executing this Code Snippet.

Conversion Constructor Output1
Conversion Constructor Output 1

4.3 Conversion Constructor: Usage 2

In the second code snippet, the single parameter Constructor is used as Conversion Constructor by using the statement like DivBy5 Obj2 = 33; The statement looks like that C++ invokes a copy Constructor for the DivBy5. By looking at the left side of the assignment statement, we know that the Object Obj2 is not yet alive. But, right side is not an Object of type DivBy5 and Hence C++ will not invoke the copy Constructor. The right side of the = operator is an integer constant 33. By combining the left and right side of the operator =, we can say that c++ will invoke the Constructor which takes an integer as a parameter. Below is the code snippet and result of it:

Conversion Constructor Output 2
Conversion Constructor Output 2

5. Closing Notes

This is a simple example where we explored the concept of Conversion Constructor. We can think of a CTime Class as a perfect example. It will take an integer representing time in seconds as a parameter to the Constructor. Then the Constructor fills the parts of Time Class like an hour, minute and seconds. For example, if 70 (seconds) passed to the Constructor, the Constructor sets 1 as minute and 10 as seconds. The usage goes like this CTime t1 = 70.

6. Complete Example

Below is the complete code example of this Article:

 

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

%d bloggers like this: