7.6 CONVERSION FUNCTIONS

Natural extension of operator overloading are conversion functions. Earlier we have seen type conversion in two different styles, automatic and by type cast. It was only for basic data types. The same concept has been brought in for objects. It uses operator overloading syntax. The “operator” function with class name as operator is known as conversion function.

Conversion functions come in three flavours. Let us discuss them one by one.

Type I

Consider the common situation of adding interest to principle amount. Here, Interest and Amount are distinct objects. To add interest to amount we have to convert interest to amount. The elegant way of doing it is to convert interest to amount first. This can be done through type ...

Get Object Oriented Programming with C++, Second Edition now with the O’Reilly learning platform.

O’Reilly members experience books, live events, courses curated by job role, and more from O’Reilly and nearly 200 top publishers.