You are on page 1of 7

Operator Overloading

C++ has the ability to provide the operators with a special meaning for a data
type. The mechanism of giving such special meanings to an operator is known as
operator overloading.
Operator overloading facilitates the specification of user-defined implementation
for operations wherein one or both operands are of user-defined class or
structure type. This helps user-defined types to behave much like the
fundamental primitive data types. Operator overloading is helpful in cases where
the operators used for certain types provide semantics related to the domain
context and syntactic support as found in the programming language. It is used
for syntactical convenience, readability and maintainability. 

Syntax of Operator Overloading:


returntype classname:: operator symbol (arglist)
{
function body //task defined
}

What is the need for operator overloading?


The meaning of an operator is always the same for the variable of basic types like
int, float, double etc. For example: To add two integers, + operator is used.
However, for user-defined types (like objects), you can redefine the way operator
works. For example:
If there are two objects of a class that contains string as its data members. You
can redefine the meaning of + operator and use it to concatenate those strings.
This feature in C++ programming that allows the programmer to redefine the
meaning of an operator (when they operate on class objects) is known as
operator overloading.
One of the best examples for Operator overloading is the “+” operator.
1. If you use “+” operator with numbers or decimal values. it just adds them
and gives the results.
2. If you use “+” operator with characters then it adds corresponding ASCI
numbers of characters.
3. if you use “+” operator with Strings then it will concatenate the values with
String.

For Example:
The below code can be replace from,
calculation = add(multiply(a, b),divide(a, b));
to
calculation = (a*b)+(a/b);

To overload an operator, a special operator function is defined inside the class as:
class className
{
... .. ...
public
returnType operator symbol (arguments)
{
... .. ...
}
... .. ...
};

-Here, returnType is the return type of the function.


-The return type of the function is followed by operator keyword.
-The symbol is the operator symbol you want to overload. Like: +, <, -, ++
-You can pass arguments to the operator function in a similar way as functions.

Example: Operator overloading in C++ Programming


#include <iostream>

using namespace std;

class Test

private:

int count;

public:

Test(): count(5){}

void operator ++()

count = count+1;

void Display() { cout<<"Count: "<<count; }

};

int main()

{
Test t; // this calls "function void operator ++()" function

++t;

t.Display();

return 0;

Output
Count: 6
Unary Operator Overloading:
The unary operators are those operators that requires only one operand to
perform different kind of operations such as increasing/decreasing a value,
negating a value etc. And overloading of such operators is called unary operator
overloading.
For e.g. unary minus(-), unary plus(+), increment(++),decrement (--), logical not(!),
pointer indirection or dereference(*), address of(&) etc.
Syntax:
Returntype operator symbol (arg)
{
function body
}

The unary operators either prefix or postfix can be overloaded with non-static
member function taking no argument or non-member function taking one
argument. After overloading the operator, it can be applied to an object in the
same way as it is applied to the basic types.

Binary Operator Overloading:


Binary operators are those operators that work on two operands. For e.g. binary
plus(+), binary minus(-), multiplication(*), division(/), greater than(>), less than(<),
equality operator(==) etc. And overloading of such operators is called binary
operator overloading.
Syntax:
return_type operator binary_operator(object_of_class)
{
body;
}

Overloading using Friend Function:


Friend function can be used as operator function in place of member function for
overloading of operators. The only difference is that a friend function requires
only one argument for unary operators and two arguments for binary operators
to be explicitly passed for it.
As the friend function is non-member function of a class, it is called without
object. Therefore, there is no calling object and all objects are passed in operator
function via arguments. As a result, there is one more argument while using
friend function as operator function than using member function.

Unary Operator Overloading using Friend Function (Non- Member Function)

End Semester Questions:


1. Discuss the advantages of having operator overloading.
2. Define operator overloading. Write a program that illustrates the concept
of unary operatory overloading and binary operator overloading. Also state
the use of friend function in case of binary operator overloading.
3. What is the advantage of using friend function for operator overloading.
Explain with appropriate syntax.
4. Why do we need to overload operators in C++? Discuss on overloading
binary operator using member function and using friend function.
5. How can you use operator overloading in C++? Give the syntax.
6. What is operator overloading? Why is it necessary to overload an operator?
Define a class string.
7. Differentiate between function overloading and operator overloading with
suitable examples.
8. What do you mean by operator overloading? Why we need to overload
operators in C++? What is the main difference between while overloading
binary operators and overloading binary operators using friend functions?

You might also like