OPERATOR
OVERLOADING IN C++
PRESENTED BY
M.RAMYA
M.Sc[CS&IT]
NSCAS
SYNOPSIS
• Introduction.
• Operator that must be overloaded as members
• Operators that cannot be overloaded
• Operator Overloading Syntax
• Implementing Operator Overloading
• Unary Operators Overloading
• Binary Operator Overloading
• Overloading Binary Operators Using Friends
• Manipulation Of String Using Operators
Introduction
Operator overloading is an important concept in C++. It is a
type of polymorphism in which an operator is overloaded to give
user defined meaning to it. Overloaded operator is used to perform
operation on user-defined data type. For example '+' operator can
be overloaded to perform addition on various data types, like for
Integer, String(concatenation) etc.
object of ostream class string
Cout << “This is test string”;
overloaded insertion operator
Operator that must be overloaded as members
• Assignment operator - =
• Subscript operator – [ ]
• Function call operator – ()
• Indirect member access operator - ->
• Indirect pointer to member access operator - ->*
Operators that cannot be overloaded
• Scope operator -::
• Sizeof
• Member selector - .
• Member pointer selector - *
• Ternary operator - ?:
Operator Overloading Syntax
Keyword Operator to be overloaded
ReturnType classname :: Operator OperatorSymbol(argument list)
{
Function body
}
Implementing Operator Overloading
Operator overloading can be done by implementing a function
which can be:
 Member Function
 Non – Member Function
 Friend Function
Operator overloading function can be a member function if the
Left operand is an Object of that class, but if the Left operand is
different, then Operator Overloading function must be a non –
member function.
Operator overloading function can be friend function if it needs
access to the private and protected members of class.
Unary Operators Overloading
The unary operators operate on a single operand and
following are the examples of Unary operators − The increment
(++) and decrement (--) operators. The Unary minus operators (-).
EXAMPLE PROGRAM
#include<iostream.h>
Using namespace std;
Class NUM
{
Private:
int n;
Public:
Void getNUM(int x) //function to get number
{
n=x;
}
Void dispNUM(void) //function to display number
{
Cout << “value of n is:” << n;
}
Void operator – (void) //unary – operator overloading
{
n= - n;
}
};
int main()
{
NUM num;
num . getNum(10); OUTPUT:
- num; Value of n is : -10
num . dispNUM();
cout << end1;
return 0;
}
Binary Operator Overloading
Overloading with a single parameter is called binary operator
overloading. Similar to unary operators, binary operators can also
be overloaded. Binary operators require two operands, and they are
overloaded by using member functions and friend functions
EXAMPLE PROGRAM
#include<iostream.h>
Using namespace std;
Class complex
{
float x; //real part
float y; //imaginary part
public:
complex() //constructor 1
{
}
complex(float real , float imag) //constructor 2
{
x=real ; y = imag;
}
complex operator + (complex);
void display (void);
};
complex complex :: operator + (complex c)
{
complex temp; //temporary
temp.x = x + c.x;
temp.y = y + c.y;
return (temp);
}
void complex :: display (void) Output:
{ C1 = 2.5 + j3.5
cout << x << “ + j ” << y << “n”; C2 = 1.6 + j2.7
} C3 = 4.1 + j6.2
int main()
{
complex C1,C2,C3;
C1 = complex(2.5,3.5);
C2 = complex(1.6,2.7);
C3 = C1 + C2;
cout << “C1 = ”; C1.display ();
cout << “C2 = ”; C2.display ();
cout << “C3 = ”; C3.display ();
return 0;
}
Overloading Binary Operators Using Friends
Friend functions may be used in the place of member
functions for overloading a binary operator, the only difference
begin that a friend function requires two arguments to be explicitly
passed to it.
For example:
string3 = string1 + string2;
if(string1 >= string2) string = string1;
EXAMPLE PROGRAM
#include<iostream.h>
#include<conio.h>
#include<stdlib.h>
class stud
{
int rno;
char *name;
int marks;
public“:
friend istream & operator>>(istream & ,stud &); //friend 1
friend void operator<<(ostream & ,stud &); //friend 2
};
MANIPULATION OF STRING USING
OPERATORS
Strings can be defined as class objects which can be then
manipulated like the built – in types. The strings vary greatly in
size, we use new to allocate memory for each strings and a pointer
variable to point to the string array. Thus we must create string
objects that can hold these two pieces of information, namely,
length and location which are necessary for string manipulations. A
typical string class will look as follows:
class string
{
char *p; //pointer to string
int len;
public:
………. //member functions
………. // to initialize and
………. // manipulate strings
};
Operator overloading

Operator overloading

  • 1.
    OPERATOR OVERLOADING IN C++ PRESENTEDBY M.RAMYA M.Sc[CS&IT] NSCAS
  • 2.
    SYNOPSIS • Introduction. • Operatorthat must be overloaded as members • Operators that cannot be overloaded • Operator Overloading Syntax • Implementing Operator Overloading • Unary Operators Overloading • Binary Operator Overloading • Overloading Binary Operators Using Friends • Manipulation Of String Using Operators
  • 3.
    Introduction Operator overloading isan important concept in C++. It is a type of polymorphism in which an operator is overloaded to give user defined meaning to it. Overloaded operator is used to perform operation on user-defined data type. For example '+' operator can be overloaded to perform addition on various data types, like for Integer, String(concatenation) etc. object of ostream class string Cout << “This is test string”; overloaded insertion operator
  • 4.
    Operator that mustbe overloaded as members • Assignment operator - = • Subscript operator – [ ] • Function call operator – () • Indirect member access operator - -> • Indirect pointer to member access operator - ->*
  • 5.
    Operators that cannotbe overloaded • Scope operator -:: • Sizeof • Member selector - . • Member pointer selector - * • Ternary operator - ?:
  • 6.
    Operator Overloading Syntax KeywordOperator to be overloaded ReturnType classname :: Operator OperatorSymbol(argument list) { Function body }
  • 7.
    Implementing Operator Overloading Operatoroverloading can be done by implementing a function which can be:  Member Function  Non – Member Function  Friend Function Operator overloading function can be a member function if the Left operand is an Object of that class, but if the Left operand is different, then Operator Overloading function must be a non – member function. Operator overloading function can be friend function if it needs access to the private and protected members of class.
  • 8.
    Unary Operators Overloading Theunary operators operate on a single operand and following are the examples of Unary operators − The increment (++) and decrement (--) operators. The Unary minus operators (-).
  • 9.
    EXAMPLE PROGRAM #include<iostream.h> Using namespacestd; Class NUM { Private: int n; Public: Void getNUM(int x) //function to get number { n=x; } Void dispNUM(void) //function to display number { Cout << “value of n is:” << n; } Void operator – (void) //unary – operator overloading { n= - n; } };
  • 10.
    int main() { NUM num; num. getNum(10); OUTPUT: - num; Value of n is : -10 num . dispNUM(); cout << end1; return 0; }
  • 11.
    Binary Operator Overloading Overloadingwith a single parameter is called binary operator overloading. Similar to unary operators, binary operators can also be overloaded. Binary operators require two operands, and they are overloaded by using member functions and friend functions
  • 12.
    EXAMPLE PROGRAM #include<iostream.h> Using namespacestd; Class complex { float x; //real part float y; //imaginary part public: complex() //constructor 1 { } complex(float real , float imag) //constructor 2 { x=real ; y = imag; } complex operator + (complex); void display (void); }; complex complex :: operator + (complex c) {
  • 13.
    complex temp; //temporary temp.x= x + c.x; temp.y = y + c.y; return (temp); } void complex :: display (void) Output: { C1 = 2.5 + j3.5 cout << x << “ + j ” << y << “n”; C2 = 1.6 + j2.7 } C3 = 4.1 + j6.2 int main() { complex C1,C2,C3; C1 = complex(2.5,3.5); C2 = complex(1.6,2.7); C3 = C1 + C2; cout << “C1 = ”; C1.display (); cout << “C2 = ”; C2.display (); cout << “C3 = ”; C3.display (); return 0; }
  • 14.
    Overloading Binary OperatorsUsing Friends Friend functions may be used in the place of member functions for overloading a binary operator, the only difference begin that a friend function requires two arguments to be explicitly passed to it. For example: string3 = string1 + string2; if(string1 >= string2) string = string1;
  • 15.
    EXAMPLE PROGRAM #include<iostream.h> #include<conio.h> #include<stdlib.h> class stud { intrno; char *name; int marks; public“: friend istream & operator>>(istream & ,stud &); //friend 1 friend void operator<<(ostream & ,stud &); //friend 2 };
  • 16.
    MANIPULATION OF STRINGUSING OPERATORS Strings can be defined as class objects which can be then manipulated like the built – in types. The strings vary greatly in size, we use new to allocate memory for each strings and a pointer variable to point to the string array. Thus we must create string objects that can hold these two pieces of information, namely, length and location which are necessary for string manipulations. A typical string class will look as follows:
  • 17.
    class string { char *p;//pointer to string int len; public: ………. //member functions ………. // to initialize and ………. // manipulate strings };