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
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:
1 2 3 4 |
//Sample 01: Private Members private: int divided; int remaining; |
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:
1 2 3 4 5 6 7 8 9 10 11 12 |
//Sample 02: Member function returns the // reminder of the division int GetReminder() { return remaining; } //Sample 03: Get the Divident int GetDivided() { return divided; } |
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:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 |
//Sample 04: A constructor with One Argument //can be used for conversion. DivBy5(int Number = 0) { if (Number == 0) { divided = 0; remaining = 0; } else { divided = Number / 5; remaining = Number % 5; } } |
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:
1 2 3 4 5 6 |
//Sample 05: Get the division 22/5 DivBy5 Obj(22); cout<<endl; cout<<endl; cout<< "Divided : " << Obj.GetDivided() << "\tReminder: " << Obj.GetReminder() << endl; |
The Picture below shows the result of executing this Code Snippet.

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:
1 2 3 4 5 6 7 |
//Sample 06: Get the division 33/5 // This time conversion is applied cout<<endl; cout<<endl; DivBy5 Obj2 = 33; cout<< "Divided : " << Obj2.GetDivided() << "\tReminder: " << Obj2.GetReminder() << endl; |

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:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 |
// TestIt.cpp : Defines the entry point for the console application. // #include "stdafx.h" #include <iostream> using namespace std; class DivBy5 { //Sample 01: Private Members private: int divided; int remaining; public: //Sample 02: Member function returns the // reminder of the division int GetReminder() { return remaining; } //Sample 03: Get the Divident int GetDivided() { return divided; } //Sample 04: A constructor with One Argument // can be used for conversion. DivBy5(int Number = 0) { if (Number == 0) { divided = 0; remaining = 0; } else { divided = Number / 5; remaining = Number % 5; } } }; int main() { //Sample 05: Get the division 22/5 DivBy5 Obj(22); cout<<endl; cout<<endl; cout<< "Divided : " << Obj.GetDivided() << "\tReminder: " << Obj.GetReminder() << endl; //Sample 06: Get the division 33/5 // This time conversion is applied cout<<endl; cout<<endl; DivBy5 Obj2 = 33; cout<< "Divided : " << Obj2.GetDivided() << "\tReminder: " << Obj2.GetReminder() << endl; } |
Categories: C++
Tags: Constructor