35 Conversion Function
Jyoti Pareek
We may recall that when we assign a value of specific data type to the variable of another type, the implicit conversion takes place.
For example if we have two variables declared as follows,
int x = 10;
float y;
We can have following assignment statements in our program y = x ; —–(i)
x = y ; —–(ii)
Here x is integer and y is float. Though no operation can be performed on different data types, above statement works because:
In case (i) x gets implicitly converted to float and assignment succeeds.
In case (ii) y gets implicitly converted to int and assignment succeeds.
What if we declare an object of Time t and an integer declared as follows
Time t;
int x;
Will this assignment succeed?
x = t or
t = x ;
No. Why not? Why here t does not get implicitly converted to int and x does not get implicitly get converted to object of Time class?
This is so what because int and float is standard / pre-defined data type provided by complier , therefore complier knows how to convert int to float or float to int or in general how to convert one standard data type into another standard data type.
But when need arises for the following conversion x = t;
Where t is a variable of user defined data type Time, the complier doesn’t know how to convert time object to integer or vice a versa.Implicit conversions of user defined data type is not defined, but C++ allows as the facility to define how we wish to convert a user defined data type(implemented as class) to standard data type or vice versa. Also we can define how to convert one user defined data type to another user defined data type (object of one class to another type).
User defined conversions can be achieved through
- (1) Constructor
- (2) Conversion function
Let us understand how following user Defined Conversions are defined for user defined data types implemented as class
- (1) Class to Standard data type
- (2) Standard data type to class
- (3) Object of one class to object of another Data type
- (1) Class to Standard Data type
This conversion can be achieved through a special function called conversion function.
The syntax of conversion function is
Operator datatype()
{
Return datatype;
}
For example if we wish to convert a time object to an integer (seconds). We can define conversion function / operator function in Time class as follows
class Time
{
int hrs,min,sec;
public:
Time() {hrs=min=sec=0;}
Time (int h, int m,int s)
{
hrs = h;
min = m;
sec = s;
}
operatorint ()
{
int seconds;
seconds = hrs*60*60 + min*60 + sec;
return seconds;
}
}
Having defined this function in Time class, following code will be successfully compiled.
void main()
{
int seconds;
Time t(2,15,30);
seconds = t; // Will work
}
(2) Standard data type to class
This Conversion can be achieved by single parameter constructor. For example if we wish to convert an integer to time object. We can define a constructor in time class which takes single integer parameter
class Time
{
int hrs, min, sec;
public:
Time(int seconds)
{
sec = seconds;
}
};
Having defined this constructor we will be able to do following assignment.
void main()
{
int seconds;
Time t;
t = seconds; // Will work
};
(3) Object of one class to object of another class
It is also possible to assign object of one class to object of another class. Let us understand it with the help of an example, if we have two class namely INR and USDOLLLAR
If we have following objects defined.
INR mymoney;
USDOLLAR yourmoney;
We can make appropriate enhancement in class INR and USDOLLAR to make following assignments work
mymoney = yourmoney
or
Yourmoney= mymoney
Following assignment can be made to work by defining of constructor in sourceor conversion function in appropriate class
Mymoney= | yourmoney | |
↓ | ↓ | |
Destination | Source | |
Destination class INR | Source class USDOLLAR |
To make this assignment work we can either write conversion function in source class or we can write constructor in destination class.If we want to modify destination class we can write constructor in destination (which is class INR in above assignment statement) class as follows
class USDOLLAR
class INR
{
int Rupee;
int paisa;
public:
INR() { Rupee = 0; paisa = 0;}
INR (USDOLLAR USD)
{
Rupee = USD.dollar /70; ___(1)
paisa = USD>cent /70; ___(2)
}
}
But in the above program statement (1) and (2) will not work. Why these statements will not work?This is because dollar and cent are private members of USDOLLAR class, therefore they can’t be accessed in constructor of INR class. So what can we do?
We can write get functions in INR class to get the values of dollar and cent data members.
class INR
{
int Rupee;
int paisa;
public:
INR (USDOLLAR USD)
{
Rupee = USD.getdollar()*70;
paisa = USD.getdollar()*70;
}
};
If we want to modify Source class, we can write conversion function in USDOLLAR class.
class USDoller
{
int doller;
int cent;
public:
—
—
operator INR()
{
INR money;
money Rupe=dollar/70; __(1)
money paisa=cent/70; __(2)
}
Here also (1) and (2) will not work. Why?
Again for the same reason that rupee and paisa are private members of INR class , therefore they are not accessible in USDOLLAR class.To make above conversion function work, we can do one of the following things
(1) By creating and returning creating unnamed INR object.
class USDOLLAR
{
int dollar;
int cent;
public:
operator INR()
{
return INR(dollar/70,cent/70);
}
}
Here we are creating unnamed INR object initializing it with values dollar/70 and cent/70.Note: this will work if we have parameterized constructor in USDOLAAR class.
(2) By writing set method in INR class.
class USDollar
{
int dollar;
int cent;
public:
operator IN()
{
INR money
money.setrupee(dollar/70);
money.setpaisa(dollar/70);
return money;
}
};
Let us summaries again
If we have declared following two objects
INR mymoney;
USDollar yourmoney;
To make following assignment statements work
mymoney =yourmoney — (1)
yourmoney =mymoney —- (2)
We can do either of the following
(1) We can write appropriate constructors in INR and USDollar class.
(2) We can write constructor in INR class to make (1) work and conversion function in INR class to make (2) work.
(3) We can write constructor in USDollar class to make (2) work and write conversion function in USDollar class to make (1) work.
(4) We can write conversion function in USDollar class and INR class to make (1) and (2) work respectively.
Summary
(1) User defined conversion can be achieved through
(i) Constructor
(ii) Conversion function
(2) Conversion from scalar data type to class type can be achieved by writing appropriate constructor in class.
(3) Conversion of class type to scalar data type can be achieved by writing conversion function in class.
(4) Class to class conversion canbe achieved by either writing constructor function in destination class or by writing operator function in source class.
Additional References
1) Stanley Lippmann, “C++ Primer”, Pearson Education.
2) Bjarne Stroustrup, “The C++ Programming Language”, Pearson Education.
3) Scott Mayer, “Effective C++”, Addison Wesley.
4) Bhushan Trivedi , Programming with ANSI C++, 2/e , Oxford University Press.
5) Yashavant P. Kanetkar “Let Us C++” , Bpb Publications.
6) Abhiram G. Ranade, “An Introduction to Programming through C++” , McGraw Hill
7) Ellis and B. Stroustrup “Annotated C++ Reference Manual”, http://www.stroustrup.com/arm.html
8) Herbert Schildt, “Complete Reference C++”, McGraw Hill Publications.
9) Ashok Kamthane, “Object Oriented Programming with ANSI and Turbo C++”, Pearson Education
10) E Balaguruswami, “Object Oriented Programming With C++”, Tata McGraw Hill
11) “C++ FAQs”, Pearson Education.