G.BALAJI MCA, M.Tech.,
Assistant Professor of Computer Science
Vivekananda College, Madurai
Operator Overloading:
 Operator – It is a symbol that indicates an operation.
Arithmetic operators are + (add two numbers), - (subtract two
numbers), * ( Multiply two numbers), / ( Divide between two
numbers).
 At now, we will take an Addition ‘+’ Sign, its use of ‘+’ sign
is
5+5=10
2.5+2.5=5
 Operator Overloading means multiple functions or multiple
jobs. In operator overloading the ‘+’ sign use of add the two
objects.
 One of C++’s great features is its extensibility, Operator
Overloading is major functionality related to extensibility.
 In C++, most of operators can be overloaded so that they can
perform special operations relative to the classes you create.
 For Example, ‘+’ operator can be overloaded to perform an
operation of string concatenation along with its pre-defined
job of adding two numeric values.
 When an operator is overloaded, none of its original meaning
will be lost.
 After overloading the appropriate operators, you can use
C++’s built in data types.
 Unary Operator
- Operators attached to a single operand.
(-a, +a, --a, ++a, a--, a++)
 Binary Operator
- Operators attached to two operand.
(a-b, a+b, a*b, a/b, a%b, a>b, a<b )
return-type class-name:: operator op(arg-list)
{
function body
}
EXPLANATION
 return type – It is the type of value returned by the specified
operation.
 op - It is the operator being overloaded. It may be unary or
binary operator. It is preceded by the keyword operator.
 operator op - It is the function name, Where operator is a
keyword.
INTRODUCTION
 In Binary operator overloading function, there should be one
argument to be passed.
 It is overloading of an operator operating on two operands.
#include<iostream>
class multiply
{
int first,second;
public:
void getdata(int a,int b)
{
first=a;
second=b;
}
Contd...,
void display()
{
cout<<“first=“<<first<<“second=“<<secon<<endl;
}
multiply operator *(multiply c);
};
void multiply::operator *(multiply c)
{
multiply temp;
temp.first=first*c.first;
temp.second=second*c.second;
return temp;
}
Contd..,
int main()
{
multiply obj1,obj2,obj3;
obj1.getdata(15,20);
obj2.getdata(3,45);
obj3=obj1*obj2;
obj3.display();
return 0;
}
Output:
45
900

Binary operator overloading

  • 1.
    G.BALAJI MCA, M.Tech., AssistantProfessor of Computer Science Vivekananda College, Madurai
  • 2.
    Operator Overloading:  Operator– It is a symbol that indicates an operation. Arithmetic operators are + (add two numbers), - (subtract two numbers), * ( Multiply two numbers), / ( Divide between two numbers).  At now, we will take an Addition ‘+’ Sign, its use of ‘+’ sign is 5+5=10 2.5+2.5=5
  • 3.
     Operator Overloadingmeans multiple functions or multiple jobs. In operator overloading the ‘+’ sign use of add the two objects.  One of C++’s great features is its extensibility, Operator Overloading is major functionality related to extensibility.  In C++, most of operators can be overloaded so that they can perform special operations relative to the classes you create.
  • 4.
     For Example,‘+’ operator can be overloaded to perform an operation of string concatenation along with its pre-defined job of adding two numeric values.  When an operator is overloaded, none of its original meaning will be lost.  After overloading the appropriate operators, you can use C++’s built in data types.
  • 5.
     Unary Operator -Operators attached to a single operand. (-a, +a, --a, ++a, a--, a++)  Binary Operator - Operators attached to two operand. (a-b, a+b, a*b, a/b, a%b, a>b, a<b )
  • 6.
    return-type class-name:: operatorop(arg-list) { function body } EXPLANATION  return type – It is the type of value returned by the specified operation.  op - It is the operator being overloaded. It may be unary or binary operator. It is preceded by the keyword operator.  operator op - It is the function name, Where operator is a keyword.
  • 7.
    INTRODUCTION  In Binaryoperator overloading function, there should be one argument to be passed.  It is overloading of an operator operating on two operands.
  • 8.
    #include<iostream> class multiply { int first,second; public: voidgetdata(int a,int b) { first=a; second=b; } Contd...,
  • 9.
    void display() { cout<<“first=“<<first<<“second=“<<secon<<endl; } multiply operator*(multiply c); }; void multiply::operator *(multiply c) { multiply temp; temp.first=first*c.first; temp.second=second*c.second; return temp; } Contd..,
  • 10.