Operator Overlosding
Operator Overlosding
Operator Overlosding
Introduction
• Operator Overloading is used to customize the behavior
of the operator.
• It provides the flexibility to give new meaning and
definitions to the existing operators.
• When the operator is overloaded, its original meaning is
not lost.
• The rules for the operators do not change.
• Syntax : declaration in class
Return_type operatorop(arglist)
● Definition:
Return_type class_name :: operator op(Arglist)
{
Function body
}
class OverloadingExample
{
private:
int m_LocalInt;
public:
OverloadingExample(int j) // default constructor
{
m_LocalInt = j;
}
int operator+ (int j) // overloaded + operator
{
return (m_LocalInt + j);
}
};
void main()
{
OverloadingExample object1(10);
int ans;
ans=object1 + 10; // overloaded operator called
cout<<ans;
}
• 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.
• Define the operator function to implement the required
operations.
Types of Operators
• Assignment Operator =
• Function call operator ()
• Array Subscript operator []
• Access to class member using pointer to object
operator ->
Operator Functions As Class Members Vs. As
Friend Functions
• Operator functions must be either member function (non-static) or
friend function.
• Friend Function will have only one argument for unary operators and
two for binary operators.
• Member function has no arguments for unary operators and only one for
binary operators
• Because the object used to invoke member function is passed implicitly
and therefore is available for member function.
• This is not the case with friend function.
• Eg:-
• A = B + 2 // valid
• A = 2 + B // not valid
Rules for Overloading Operators
• Only existing operators can be overloaded. New operators
cannot be created.
• The overloaded operator must have at least one operand
that is of user-defined type.
• We cannot change the basic meaning of an operator. That
is to say, we cannot redefine the plus (+) operator to
subtract one value from other.
• Overloaded operators follow the syntax rules of the original
operators. They cannot be overridden.
• There are some operators that cannot be overloaded.
• We cannot use friend functions to overload certain
operators (=,(),[],->) but we can use member function to
overload them.
• Unary operators, overloaded by means of a member
function, take no explicit arguments and return no explicit
values, but, those overloaded by means of a friend
function, take on reference argument i.e the object of that
class.
• Binary operators overloaded using member functions take
one explicit argument and two arguments when friend
function is used.
• When using binary operators overloaded through a
member function, the left hand operator must be an object
of the relevant class.
• Binary arithmetic operators such as +,-,* and / must
explicitly return a value.
• Overloading () operator
• #include<iostream.h>
• Class Point
• {
Int x;
Int y;
Public:
void operator ()(int tempX, int tempY)
{
x = tempX; y = tempY;
}
};
Void main()
{
Point P1, P2;
p1(2,3);
p2(4,5);
• Example :
• Class Integer
• {
private: int value;
public: friend ostream & operator << (ostream &, Integer &);
friend ostream & operator >>(istream &, integer &);
Integer(int TempVal=0)
{ value = TempVal; }
operator int()
{ return value; }
};
// << and >> overloading definitions
Void main()
{
Integer INT1 = 5; // constructor applied.
Integer INT2;
int int1;
int int2 = 7;