ArtsAutosBooksBusinessEducationEntertainmentFamilyFashionFoodGamesGenderHealthHolidaysHomeHubPagesPersonal FinancePetsPoliticsReligionSportsTechnologyTravel
  • »
  • Technology»
  • Computers & Software»
  • Computer Science & Programming»
  • Programming Languages

Conversion Constructor In C++ with Example

Updated on June 14, 2016
sirama profile image

I am a software engineer. I have been come across C++,MFC, .net technologies. I like playing video games & reading books.

1. What is Conversion Constructor?

In this Article, I will show how do we use constructor as converters. We can say a constructor as converter when the constructor is having a single argument. For Example look at the below Code:

Source

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 also. The conversion is from Boat to Train. In this article, we will see an example that makes use of this Conversion Constructor. Let us go with the Example:

2. Private Members

The class DivBy5 has two members. The purpose of this class is to keep the divided and remaining portion of a number when it will be divided by Five. The member variables divided and remaining will hold the result of the division by five. Below are the members:

	//Sample 01: Private Members
private:
	int divided;
	int remaining;

3. Member Functions

DivBy5 class supports two member functions are called GetReminder and GetDivided. These two functions return the corresponding private members of the class DivBy5. The code is shown below:

//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

As Already told a class with a single parameter can be used as conversion. The constructor shown below takes a single integer as a parameter and constructs the class object of type DivBy5. When nothing is passed to the constructor the member contains the value zero. When we pass some integer number the dividend is stored in the divided member and remaining is stored in the other member of the class object.

The division operator is applied to the supplied number to get the divided value. And the Modulus (%) operator is used to hold the remainder. These two operators, I mean the division (/) and the Modulus (%) are used when we have a valid number supplied to the constructor.

Below is the code:

//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;
	}
}

5. Usage

In the First Code snippet (//Sample 05) we used the constructor normally. That is we create the object by passing the integer value to it like DivBy5 Obj(22). The result of executing the first code snippet is shown below:

Source
//Sample 05: Get the division 22/5
DivBy5 Obj(22);
cout<< "Divided : " << Obj.GetDivided() 
	<< "\tReminder: " << Obj.GetReminder() << endl;

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 a copy constructor for the DivBy5 is invoked. By looking at the left side of the assignment statement we know that the Object Obj2 is not yet constructed. But, right side is not an object of type DivBy5 and Hence the copy constructor will not be invoked. The right side of the = operator is an integer constant 33. By combining the left and right side of the operator = (Left side Object is to be constructed, right side is an integer) we can clearly say that constructor that takes the integer as a parameter will be invoked. Below is the code snippet and result of it:

Source
//Sample 06: Get the division 33/5 
// This time conversion is applied
DivBy5 Obj2 = 33;
cout<< "Divided : " << Obj2.GetDivided() 
	<< "\tReminder: " << Obj2.GetReminder() << endl;

This is a simple example to demonstrate the concept. Think of a CTime class taking an integer as Seconds and filling the parts of time class like Hour, Minute, Seconds. Ok, I am leaving it to the readers to implement it and below is the complete code Example for this Hub Page Article:

// 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;
}

Comments

    0 of 8192 characters used
    Post Comment

    • profile image

      Rwik Mukhopadhyay 4 years ago

      Great article

    • profile image

      reza mohajer 3 years ago

      so so so useful!

      thank you!

    Click to Rate This Article