CHAPTER 7: Operator Overloading and Type Conversion: by Darshit Shah Page 1 of 6
CHAPTER 7: Operator Overloading and Type Conversion: by Darshit Shah Page 1 of 6
Introduction:
We use different operators like +, -, * on basic data types like int, float or double. Can we use them
for user defined data types like arrays, structures or classes? E.g.
In order to join two strings, we write following code.
char name1[30] = “Nirma ” ;
char name2[30] = “University”;
char fullname[60];
strcpy (fullname,name1);
strcat (fullname, name2);
Instead of the above program, can we write,
fullname = name1 + name2;
Look at the following program listing. It contains various operators being overloaded for a class
complex.
#include <iostream.h>
#include <conio.h>
class complex
{
float r, i;
public:
};
complex add ( complex c1 , complex c2)
//complex operator + ( complex c1 , complex c2)
{
complex c3;
c3.r = c1.r + c2.r;
c3.i = c1.i + c2.i;
return c3;
}
complex complex::operator + ( complex c)
{
complex t;
t.r = r + c.r;
t.i = i + c.i;
return t;
}
istream & operator >> (istream & stream, complex & c)
{
cout << "Enter values of Real & Imaginary :";
stream >> c.r >> c.i;
return stream;
}
ostream & tab (ostream & stream) // Creating your own manipulator.
{
stream << "\t";
return stream;
}
void main()
{
complex c1,c2,c3,c4,c5;
cin >> c1 >> c2;
c3 = c1.add(c2);
c4 = c1 + c2; // Internally converted as c4 = c1.operator +(c2);
c5 = add(c1,c2);
cout << "c1 = " << c1 << "c2 = " << c2;
cout << "c3 = " << c3 << "c4 = " << c4 << "c5 = " << c5;
c4 = c1 + c2 * c3; // c4 = c1.operator+ (c2.operator *(c3));
cout << "c4 = " << c4;
--c1;
c1--; // This line generates warning.
c2++;
-c3;
cout << "c1 = " << c1 << "c2 = " << c2 << "c3 = " << c3;
cout << tab << "c1 = " << c1 << tab << "c1+c2 " << c1+c2 << endl;
}
Operator function must be either member functions or friend functions. A basic difference between
them is that a friend function will have only one argument for unary operators and two arguments
for binary operators while member function will have no argument for unary operators and one
argument for binary operators.
Type Conversion:
#include <iostream.h>
#include <conio.h>
class weight
{
int kg, gram;
public:
// Constructor function used to convert basic data-type to user-defined
// data-type.
weight(int g)
{
kg = g / 1000;
gram = g % 1000;
}
// Function to convert Class data-type to basic data-type.
operator int()
{
return (kg * 1000 + gram);
}
void display(void)
{
cout << kg << " Kilogram & " << gram << " Gram " << endl;
}
};
void main()
{
int g = 3500, gram;
clrscr();
weight w1(g); // Conversion from built-in to user-defined data type.
w1.display();
weight w2 = g; // Conversion from built-in to user-defined data type.
w2.display();
gram = w2; // Conversion from user-defined to built-in data type.
cout << "Gram = " << gram;
getch();
}
In the above example, weight w1(g); and weight w2 = g; these two lines calls the constructor
function of class weight. The constructor function converts grams into kilogram and grams and
assigns the values to variables kg & gram respectively.
#include <iostream.h>
#include <conio.h>
class weightgram
{
int gram;
public:
weightgram(int g)
{
gram = g;
}
weightgram(float kg)
{
gram = kg * 1000;
}
void display(void)
{
cout << gram << " Grams" << endl;
}
};
class weightkg
{
float kg;
public:
weightkg(float kg)
{
weightkg::kg = kg;
};
operator weightgram()
{
weightgram g(kg);
return g;
}
void display(void)
{
cout << kg << " Kilogram" << endl;
}
};
In the above program, k is the object of source class while g is the destination
class object. The conversion is achieved by specifying casting operator function
in the source class weightkg.