Operator
Overloading
Operator Overloading
• Provides a flexible option for the creation of new definitions for most
of the C++ operators.
• All the C++ operators can be overloaded except the following:
• Class member access operators (., .*)
• Scope resolution operator (::)
• Size operator (sizeof)
• Conditional operator (?:)
Defining Operator Overloading
• To define additional task to an operator, its meaning in relation to the class
to which the operator is applied must be specified.
• A special function, called the operator function, is used to describe the task.
• The general form of an operator function is:
return type classname :: operator op(arglist)
{
Function body //task defined
}
where return type is the type of value returned by the specified operation
and op is the operator being overloaded.
• The op is preceded by the keyword operator. operator op is the function
name
• Operator functions must be either member functions or friend
functions.
• A friend function will have only one argument for unary operators and
two for binary operators while a member function has no arguments
for unary operators and only one for binary operators.
• The object used to invoke the member function is passed implicitly
and therefore is available for the member function.
• Arguments may be passed either by value or by reference.
• The process of overloading involves the following steps:
• Create a class that defines the data type that is to be used in the overloading
operation.
• Declare the operator function operator op() in the public part of the class.
• It may be either a member function or a friend function.
• Define the operator function to implement the required operation.
• Overloaded operator functions can be invoked by expressions such as
op x or x op
for unary operators and
x op y
for binary operators.
Overloading unary operators
// overloading unary minus
#include<iostream>
using namespace std;
class space
{
int x, y, z;
public:
void getdata(int a, int b, intc);
void display(void);
void operator –(); //overload unary minus
};
void space :: getdata(int a, int b, int c)
{ x=a; y=b; z=c; }
void space :: display(void)
{
cout << x << “ ”;
cout << y << “ ”;
cout << z << “ ”;
}
void space :: operator-()
{ x = -x;
y = -y;
z = -z;
}
int main()
{
space S;
S.getdata(10, -20, 30);
cout << “S: ”;
S.display();
-S;
cout << “S: ”;
S.display();
return 0; Output:
S: 10 -20 30
} S: -10 20 -30
• The function operator –() takes no argument. It changes the sign of
data members of the object S.
• It is possible to overload a unary minus operator using a friend
function as follows:
friend void operator-(space &s); //declaration
void operator-(space &s) //definition
{
s.x = -s.x;
s.y = -s.y;
s.z = -s.z;
}
Overloading binary operators
#include <iostream>
using namespace std;
class complex
{
float x, y;
public:
complex () { } //constructor 1
complex(float real, float imag) //constructor 2
{ x = real; y = imag; }
complex operator+(complex c);
void display(void);
};
complex complex :: operator+(complex c)
{
complex temp;
temp.x = x + c.x;
temp.y = y + c.y;
return temp;
}
void complex :: display(void)
{
cout << x << “ + j” << y << “\n”;
}
int main()
{
complex c1, c2, c3; //constructor 1 invoked
c1 = complex(2.5, 3.5); //constructor 2 invoked
c2 = complex(1.6, 2.7);
c3 = c1 + c2;
cout << “c1 = ”;
c1.display()
cout << “c2 = ”;
c2.display()
cout << “c3 = ”; Output:
c3.display() c1 = 2.5 + j3.5
return 0; c2 = 1.6 + j2.7
c3 = 4.1 + j6.2
}