C++ Operators Overloading
Operator overloading is a compile-time polymorphism in which the
operator is overloaded to provide the special meaning to the user-
defined data type. Operator overloading is used to overload or
redefines most of the operators available in C++. It is used to perform
the operation on the user-defined data type. For example, C++ provides
the ability to add the variables of the user-defined data type that is
applied to the built-in data types.
The advantage of Operators overloading is to perform different
operations on the same operand.
Operator that cannot be overloaded are as follows:
o Scope operator (::)
o Sizeof
o member selector(.)
o member pointer selector(*)
o ternary operator(?:)
Syntax of Operator Overloading
1) Declaration
Return type operator op()
Body
2)Defin oprator function
return_type class_name : : operator op(argument_list)
{
// body of the function.
}
Where the return type is the type of value returned by the
function.
class_name is the name of the class.
operator op is an operator function where op is the operator
being overloaded, and the operator is the keyword.
3)Calling/invoke operator
Whenever we want to call operator overloading function.
Syntax
Operator sign objectname;
Ex
-s1;
Operator Overloading can be done by using three approaches, they
are
1. Overloading unary operator.
2. Overloading binary operator.
3. Overloading binary operator using a friend function.
Rules for Operator Overloading
o Existing operators can only be overloaded, but the new operators
cannot be overloaded.
o The overloaded operator contains atleast one operand of the
user-defined data type.
o We cannot use friend function to overload certain operators.
However, the member function can be used to overload those
operators.
o When unary operators are overloaded through a member
function take no explicit arguments, but, if they are overloaded by
a friend function, takes one argument.
o When binary operators are overloaded through a member
function takes one explicit argument, and if they are overloaded
through a friend function takes two explicit arguments.
Overloading Unary Operator: Let us consider to overload
1)
(-) unary operator. In unary operator function, no
arguments should be passed. It works only with one class
objects. It is a overloading of an operator operating on a
single operand.
o Example:
Write a program to overload unary minus(-) operator.
#include<iostream.h>
#include<conio.h>
class temp
{
int x,y,z;
public:
void getdata(int a,int b,int c)
{
x=a;
y=b;
z=c;
}
void display()
{
cout<<”x=”<<x<<endl;
cout<<”y=”<<y<<endl;
cout<<”z=”<<z<<endl;
}
void operator –() // operator overloading function
{
x=-x;
y=-y;
z=-z;
}
};
temp t;
t.getdata(10,-20,30);
cout<<”before overloading”;
t.display();
-t; // invoke operator overloading function
cout<<”after overloading”;
t.display();
getch();
return 0;
}
out put
before overloading
x=10
y=-20
z=30
after overloading
x=-10
y=20
z=-30
Overloading Binary + Operator
It is an overloading of an operator operating on two operands. Let’s
take the same example of class Height, but this time, add two Height
objects h1 and h2.
#include <iostream>
#include<conio.h>
class Height
{
public:
int feet, inch;
Height() // default constructor
{
feet = 0;
inch = 0;
}
Height(int f, int i) //parameterized constructor
{
feet = f;
inch = i;
}
// Overloading (+) operator to perform addition of
// two distance object using binary operator
Height operator+(Height& d2) // Call by reference
{
Height h3; // Create an object to return
h3.feet = feet + d2.feet; // Perform addition of feet and inches
h3.inch = inch + d2.inch;
return h3; // Return the resulting object
}
};
int main()
{
Height h1(3, 7);
Height h2(6, 1);
Height h3;
h3 = h1 + h2; //Use overloaded operator
cout << "Sum of Feet & Inches: " << h3.feet << "'" << h3.inch << endl;
getch();
return 0;
}
Output: