• In my previous post I had explained how “Class Type to Basic Conversion” can be done using both constructor and special casting operator function. In this post I will show you how we can perform conversion from one class type to another class type.
  • In this type of conversion both the type that is source type and the destination type are of class type. MeansĀ  the source type is of class type and the destination type is also of the class type. In other words, one class data type is converted into the another class type.
  • For example we have two classes one for “computer” and another for “mobile”. Suppose if we wish to assign “price” of computer to mobile then it can be achieved by the statement below which is the example of the conversion from one class to another class type.
mob = comp ;   // where mob and comp are the objects of mobile and computer classes respectively.
  • Here the assignment will be done by converting “comp” object which is of class type into the “mob” which is another class data type.
  • Conversion from one class to another class can be performed either by using the constructor or type conversion function.

Using Conversion Function

  • The following program demonstrates conversion from one class to another class type with the help of conversion function where, we have overloaded “=” operator for conversion purpose.
/* Program to convert one class to another class 

#include "iostream.h"
#include "conio.h"
#include "iomanip.h"

class inventory1
{
	int ino,qty;
	float rate;
	public:
		inventory1(int n,int q,float r)
		{
			ino=n;
			qty=q;
			rate=r;
		}
		inventory1()
		{
			cout<<"\n Inventory1's Object Created";
		}
		int getino()
		{
			return(ino);
		}
		float getamt()
		{
			return(qty*rate);
		}
		void display()
		{
			cout<<endl<<"ino = "<<ino<<" qty = "<<qty<<" rate = "<<rate;
		}
};
class inventory2
{
	int ino;
	float amount;
	public:
	
	void operator=(inventory1 I)
	{
		ino=I.getino();
		amount=I.getamt();
	}
	void display()
	{
		cout<<endl<<"ino = "<<ino<<" amount = "<<amount;
	}
};
void main()
{
	clrscr();
	inventory1 I1(1001,30,75);
	inventory2 I2;
	I2=I1;
	//inventory2 I2=I1;
	I1.display();
	I2.display();
	getch();
}
  • In the below program we have two classes “Time” and “Minute” respectively in which we have tried to convert from Time class to Minute class.
/* Program to convert class Time to another class Minute. 

#include "iostream.h"
#include "conio.h"
#include "iomanip.h"
class Time
{
	int hrs,min;
	public:
		Time(int h,int m)
		{
			hrs=h;
			min=m;
		}
		Time()
		{
			cout<<"\n Time's Object Created";
		}
		int getMinutes()
		{
			int tot_min = ( hrs * 60 ) + min ;
                        return tot_min;
		}
		void display()
		{
			cout<<"Hours: "<<hrs<<endl ;
                        cout<<" Minutes : "<<min <<endl ;
		}
};
class Minute
{
	int min;
	public:
	
        Minute()
        {
           min = 0;
        }
	void operator=(Time T)
	{
		min=T.getMinutes();
	}
	void display()
	{
		cout<<"\n Total Minutes : " <<min<<endl;
	}
};
void main()
{
	clrscr();
	Time t1(2,30);
        t1.display();
	Minute m1;
        m1.display();
        
	m1 = t1;    // conversion from Time to Minute
	
	t1.display();
	m1.display();
	getch();
}