You are on page 1of 12

Compile Time Polymorphism

- Operator overloading

P. Vasuki
Operator Overloading
• Defining additional functionalities to the operator.
• Operators works for basic data type. Overloading
makes the operator works for user defined data type
(objects).
• To define an additional task to an operator, the
operator functionality has to be defined in relation to
the class.
Operator Overloading
• This is done with the help of a special function called
operator function.
return type class-name : :operator op (arg-list)
{
Function body // task defined
}
Example:
complex complex: :operator + (complex c)
{
complex result;
result.real = real + c.real;
result.img = img + c.img;
return result;

}
Definition of Operator Overloading
Function
Operator Function must be either
– member function (non-static)
Or
– friend function.
The basic difference :
• A friend function will have only one argument for
unary operators and two for binary operators.
• A member function has no arguments for unary
operators and one argument for binary operators.
• This is because 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.
Sample – Overloading member
function
class complex //Overloading using member function
{
complex operator + (complex c);
}
//usage
int main()
{
complex c1,c2,sum;
sum = c1+c2;
}
Sample – Overloading Non member function
class complex //Overloading using non member
function
{
friend complex operator+(complex c1, complex c2)
}
//Global function
complex operator + (complex c1, complex c2)
//usage
int main()
{
complex c1,c2,sum;
sum = c1+ c2;
}
Unary Operator Overloading
Consider a unary minus operator:
– It takes just one operand.

– Changes sign of data item.

– The unary minus when applied to an


object should change the sign of each
of its data items.
Sample - pre increment and post increment
// Pre-increment Operator
complex complex::operator++()
{
real++; img++;
return *this;
}

// Post-increment Operator
complex complex::operator++(int)
{
const complex old(*this);
this->real++; this->img ++; //increment current obj value
return old; //return object with older value.
}
Operators – can’t be overloaded using
friend function

• Assignment operator =
• Function call operator( )
• Subscripting operator [ ]
• Class member access operator ->
Subscript operator overloading
int &operator[](int i) //May be used to check
//indices are in boundary
{ if( i > SIZE )
{ cout << "Index out of bounds" <<endl;
// return first element. Or throw error
return arr[0];
}
return arr[i];
}
Operators that cannot be
overloaded
– Class member access operators ( . & .*)
– Scope resolution operators ( : : )
– Size operator (sizeof)
– Conditional operators (? : )
Suggested Reading

• Overloading () operator
• Overloading <> operator
• Overloading new and delete operator

• Ref: . B. Trivedi, “Programming with


ANSI C++”, Oxford University

You might also like