Operator Overloading in C++
Operator overloading is another example of C++ polymorphism. In fact, some form of operator overloading is available in all high level languages. For example, in BASIC the + operator can be used to carry out three different operations such as adding integers, adding two real numbers and concatenating two strings. Operator overloading is one of the most challenging and exciting features of C++. The main advantages of using overloading operators in a program are that it is much easier to read and debug.
Operators which already exist in the language, can only be overloaded. Overloading cannot alter either the basic template of an operator, nor its place in the order of precedence. It seems that the main idea of using operator overloading in a C++ program is to make it more natural to read and write. Even debugging such codes are much easier. As C++ is mostly used to develop a large software package in an easy way, due care must be taken while implementing the operator overloading in a program, otherwise, it leads to confusion while debugging and testing the codes. Operator overloading is accomplished by means of a special kind of function. Operator overloading can be carried out by means of either member functions or friend functions.
The general syntax of operator overloading is,
return_type operator operator_to_be_overloaded (parameters);
The keyword ‘operator’ must be preceded by the return type of a function which gives information to the compiler that overloading of operator is to be carried out. Only those operators that are predefi ned in the C++ compiler are allowed to be overloaded.
Following are some examples for operator overloading of functions, their declaration and their equivalent conventional declaration.
1. void operator++ (); is equal to
void increment();
2. int sum (int x,int y); is equal to
int operator+ ( int x, int y);
Example 1.
A program to create a class of objects, namely, obja and objb. The contents of object obja is assigned to the object objb using the conventional assignment technique.
//using an assignment operator
//without overloading
#include <iostream>
using namespace std;
class sample {
private :
int x;
oat y;
public :
sample(int, oat);
void display();
};
sample :: sample ( int one, oat two)
{
x = one;
y = two;
}
void sample :: display()
{
cout << “ integer number (x) = ” << x << endl;
cout << “ oating value (y) = ” << y << endl;
cout << endl;
}
int main()
{
sample obj1(10,-22.55);
sample obj2(20,-33.44);
obj2 = obj1;
cout << “ contents of the rst object \n”;
obj1.display();
cout << “ contents of the second object \n”;
obj2.display();
return 0;
}
Output of the above program
contents of the rst object
integer number (x) = 10
oating value (y) = -22.55
contents of the second object
integer number (x) = 10
oating value (y) = -22.55
Example 2.
A program to create a class of objects, namely, obja and objb. The contents of object obja is assigned to the object objb using the operator overloading technique.
//overloading an assignment operator
#include <iostream>
using namespace std;
class sample {
private :
int x;
oat y;
public :
sample(int, oat);
//overloading assignment operator
void operator= (sample abc);
void display();
};
sample :: sample (int one, oat two)
{
x = one;
y = two;
}
void sample :: operator= (sample abc)
{
x = abc.x;
y = abc.y;
}
void sample :: display()
{
cout << “ integer number (x) = :” << x << endl;
cout << “ oating value (y) = :” << y << endl;
cout << endl;
}
int main()
{
sample obj1(10,-22.55);
sample obj2(20,-33.44);
obj1.operator =(obj2);
cout << “ contents of the rst object \n”;
obj1.display();
cout << “ contents of the second object \n”;
obj2.display();
return 0;
}
Output of the above program
contents of the rst object
integer number (x) = :20
oating value (y) = :-33.44
contents of the second object
integer number (x) = :20
oating value (y) = :-33.44
Example 3.
A program to create a class of objects, namely, obja and objb. The contents of object obja is assigned to the object objb using the operator overloading technique and the conventional method to access the overloaded operator function.
//overloading an assignment operator
#include <iostream>
using namespace std;
class sample {
private :
int x;
oat y;
public :
sample(int, oat);
void operator= (sample abc);
void display();
};
sample :: sample (int one, oat two)
{
x = one;
y = two;
}
void sample :: operator= (sample abc)
{
x = abc.x;
y = abc.y;
}
void sample :: display()
{
cout << “ integer number (x) = :” << x << endl;
cout << “ oating value (y) = :” << y << endl;
cout << endl;
}
int main()
{
sample obj1(10,-22.55);
sample obj2(20,-33.44);
obj1 = obj2;
cout << “ contents of the rst object \n”;
obj1.display();
cout << “ contents of the second object \n”;
obj2.display();
return 0;
}
Output of the above program
contents of the rst object
integer number (x) = :20
oating value (y) = :-33.44
contents of the second object
integer number (x) = :20
oating value (y) = :-33.44

0 comments
Post a Comment