Professional Documents
Culture Documents
Chapter 5
Chapter 5
Operator overloading
Operator overloading is one of the features of C++ language. The concept by which we can give
special meaning to an operator of language is known as operator overloading. For example +
operator in C++ work only basic type like int and float means; c=a+b is calculated by compiler if
a, b and c are basic type. Suppose that if a, b and c are objects of user defined class compiler give
error. However using operator overloading we can make this statement legal even if a, b and c
are object of class. Therefore operator overloading enabling C++’s operators to work with class
objects, since it permit us to define the behaviour of operators when applied to objects of a class.
C++ allows the programmer to extend the definitions of most of the operators to work
with classes by overloading the operator and to define additional meanings for its predefined
operators by overloading them. There are two types of operators to overload:
1. Unary
2. Binary
Unary operators are the ones that require only one operator to work. Unary operators are applied
to the left of the operand. For example, ^, &, ~and !.
Binary operators require two operands on both sides of the operator. +,-,*,/,%,=,< and >are
examples of binary operators.
5.1. Declaration of operator overloading
The declaration of operator overloading is done with the help of a special function called
operator function. The operator is a keyword in C++.the general format of the operator function
is:
return_type operator oper(Parameter list){
body of the function which defined task
}
In the above declaration operator is a keyword and oper is the operator to be used in the function
like +,-,*, etc.
Here you have an example that includes the operator +. We are going to sum the bi-dimensional
vectors a (3,1) and b(1,2). The addition of two bi-dimensional vectors is an operation as simple
as to add the two x coordinates to obtain the resulting x coordinate and to add the two y
#include <iostream.h>
class CVector {
public:
int x,y;
CVector () {
};
CVector (int,int);
CVector operator + (CVector);
};
CVector::CVector (int a, int b) {
x = a;
y = b;
}
CVectorCVector::operator+ (CVector param) {
CVector temp;
temp.x = x + param.x;
temp.y = y + param.y;
return (temp);
}
int main () {
CVector a (3,1);
CVector b (1,2);
CVector c;
c = a + b;
cout << c.x << "," << c.y;
return 0;
}
The following table 5.1. Show a summary on how the different operator functions must be
declared (replace @ by the operator in each case):
+-*/%^&|<
a@b > == != <= >= A::operator@(B) operator@(A, B)
<< >> && || ,
= += -= *= /=
a@b %= ^= &= |= A::operator@(B) -
<<= >>= [ ]
Table 5.2
Generally the above listed operator in the table 5.2 can be overloaded in the user defined class
whereas the following operators are not overloaded in C++ as shown in table 5.3
To prevent a member function from changing an implicit parameter, add the word const to
the function.
void Point::move (double dx, double dy) const; // Can’t make changes to x, y
void Point::move (double dx, double dy); // Can change the implicit parameters x, y
For non-member functions, we have to add an & to change the value of an explicit
parameter.
void move (double& dx, double& dy); // Can change explicit reference parameters dx, dy
void move (double dx, double dy); // Can’t change explicit value parameters dx, dy
The following table 5.4 the summary of explicit and implicit variables with respect to the values
changes
The operator+ ( ) function has only one parameter even though it overloads the binary +
operator. The reason that operator+ ( ) function takes only one parameter is that the