• In the previous post we had seen about the basic concept of type conversion. In this post I will show you how type conversion can be achieved. First we will deal with the “Basic type to Class type Conversion”.
  • As mentioned before, in this type of conversion the source type is basic type and the destination type is class type. Means basic data type is converted into the class type.
  • For example we have class employee and one object of employee ‘emp’ and suppose we want to assign the employee code of employee ‘emp’ by any integer variable say ‘Ecode’ then the statement below is the example of the conversion from basic to class type.
emp = Ecode ;
  • Here the assignment will be done by converting “Ecode” which is of basic or primary data type into the class type.
  • The conversion from basic type to the class type can be performed by two ways:
  1. Using constructor
  2. Using Operator Overloading

Using Constructor

  • We can use constructor to perform type conversion during the object creation.
  • Consider the following example with class ‘Time’ in which we want to assign total time in minutes by integer variable ‘duration’.
  • To achieve that we have implemented one constructor function which accepts one argument of type integer as follow:
/* Program to convert basic type to class type using constructor */

#include "iostream.h"
#include "conio.h"
class Time
{
	int hrs,min;
	public:
                Time(int);
		void display();
};

Time :: Time(int t)
{
	cout<<"Basic Type to ==> Class Type Conversion..."<<endl;
	hrs=t/60;
	min=t%60;
}

void Time::display()
{
	cout<<hrs<< ": Hours(s)" <<endl;
	cout<<min<< " Minutes" <<endl;
}

void main()
{
	clrscr();
	
	int duration;
	cout<<"Enter time duration in minutes"; 
        cin>>duration;
	
	Time t1=duration;
        
	t1.display();
	getch();
}
  • Here, we have created an object “t1” of class “Time” and during the creation we have assigned integer variable “duration”. It will pass time duration to the constructor function and assign to the “hrs” and “min” members of the class “Time”.
  • We have to note that during type conversion using the constructor we can pass only one argument and we can do type conversion at the type of initialization only.

Using Operator Overloading

  • We can also achieve type conversion by operator overloading.
  • We can overload assignment operator for this purpose.
  • Above example of Time class can be rewritten for type conversion using operator overloading concept to overload the assignment operator (=) as follow:
/* Program to convert from basic type to class type using operator overloading */

#include "iostream.h"
#include "conio.h"
class Time
{
	int hrs,min;
	public:
		void display();
		void operator=(int); // overloading function
};
void Time::display()
{
	cout<<hrs<< ": Hour(s) "<<endl ;
	cout<<min<<": Minutes"<<endl ;
}

void Time::operator=(int t)
{
	cout<<"Basic Type to ==> Class Type Conversion..."<<endl;
	hrs=t/60;
	min=t%60;
}

void main()
{
	clrscr();
	Time t1;
	int duration;
	cout<<"Enter time duration in minutes";
        cin>>duration;
	cout<<"object t1 overloaded assignment..."<<endl;
	t1=duration;
	t1.display();
	cout<<"object t1 assignment operator 2nd method..."<<endl;
	t1.operator=(duration);
	t1.display();
	getch();
}
  • By using overloaded assignment operator we can perform the type conversion at any place in program.