SlideShare a Scribd company logo
1 of 11
Operator Overloadng
Operator Overloading

• Each C++ operator has a predefined meaning. Most of them
  are given additional meaning through the concept called
  operator overloading
• Main idea behind Operator overloading is to use C++
  operators with the class objects.
Eg:
      ‘+’ can be used to add 2 ints, floats or doubles, the
  same ‘+’ can be used to add 2 class objects, thereby ‘+’ gets
  overloaded.
• When an operator is overloaded, its original meaning is not
  lost.
C++ provides a wide variety of operators to
perform operations on various operands.
return type class-name : :operator op (arg-list)
{
   Function body // task defined
}
• return type is the type of value returned by the specified
  operation.
• op is the operator being overloaded.It may be a unary or binary
  operator
• op is preceded by the keyword operator.
• operator op is the function name.
The process of overloading involves the
following 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. It may be either a
    member function or a friend function.

  – Define the operator function to implement the
    required operations.
Operator Function must be either
  – member function (non-static)
  Or
  – friend function.
  The basic difference :
       • A friend function will have only one argument for
         unary operators and two for binary operators.
       • A member function has no arguments for unary
         operators and one argument for binary operators.
       • This is because the object used to invoke the member
         function is passed implicitly and therefore is available
         for the member function.
       • Arguments may be passed either by value or by
         reference.
Overloaded operator functions can be invoked by
expressions such as:
For unary operators: op x or x op
For binary operators: x op y

op x or x op would be interpreted as
   for a friend function: operator op (x)
  for a member function: x.operator op ( )
x op y would be interpreted as
  for a friend function: operator op (x,y)
  for a member function: x.operator op (y)
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.


– Overloaded operators follow the syntax rules of the
  original operators.
Rules For Overloading Operators
                         continue…

– The following operators that cannot be overloaded:
   • Size of     Size of operator
   •   .         Membership operator
   •   .*        Pointer-to-member operator
   •   ::        Scope resolution operator
   •   ? ; Conditional operator
Rules For Overloading Operators
                         continue…

– The following operators can be over loaded with the use of
  member functions and not by the use of friend functions:
    • Assignment operator =
    • Function call operator( )
    • Subscripting operator [ ]
    • Class member access operator ->
– 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 one reference argument.
Rules For Overloading Operators
                         continue…

– Binary operators overloaded through a member function
  take one explicit argument and those which are overloaded
  through a friend function take two explicit arguments.
– When using binary operators overloaded through a
  member function, the left hand operand must be an object
  of the relevant class.
– Binary arithmetic operators such as +, -, * and / must
  explicitly return a value. They must not attempt to change
  their own arguments.

More Related Content

What's hot

Presentation on overloading
Presentation on overloading Presentation on overloading
Presentation on overloading
Charndeep Sekhon
 
08 c++ Operator Overloading.ppt
08 c++ Operator Overloading.ppt08 c++ Operator Overloading.ppt
08 c++ Operator Overloading.ppt
Tareq Hasan
 
Operator overloading
Operator overloadingOperator overloading
Operator overloading
abhay singh
 
#OOP_D_ITS - 5th - C++ Oop Operator Overloading
#OOP_D_ITS - 5th - C++ Oop Operator Overloading#OOP_D_ITS - 5th - C++ Oop Operator Overloading
#OOP_D_ITS - 5th - C++ Oop Operator Overloading
Hadziq Fabroyir
 

What's hot (20)

operator overloading in C++
operator overloading in C++operator overloading in C++
operator overloading in C++
 
Bca 2nd sem u-4 operator overloading
Bca 2nd sem u-4 operator overloadingBca 2nd sem u-4 operator overloading
Bca 2nd sem u-4 operator overloading
 
Operator overloading and type conversions
Operator overloading and type conversionsOperator overloading and type conversions
Operator overloading and type conversions
 
c++
c++c++
c++
 
Operator overloading
Operator overloadingOperator overloading
Operator overloading
 
Operator overloading in C++
Operator  overloading in C++Operator  overloading in C++
Operator overloading in C++
 
Operator Overloading
Operator OverloadingOperator Overloading
Operator Overloading
 
operator overloading & type conversion in cpp over view || c++
operator overloading & type conversion in cpp over view || c++operator overloading & type conversion in cpp over view || c++
operator overloading & type conversion in cpp over view || c++
 
Operator overloading
Operator overloading Operator overloading
Operator overloading
 
Presentation on overloading
Presentation on overloading Presentation on overloading
Presentation on overloading
 
14 operator overloading
14 operator overloading14 operator overloading
14 operator overloading
 
Unary operator overloading
Unary operator overloadingUnary operator overloading
Unary operator overloading
 
08 c++ Operator Overloading.ppt
08 c++ Operator Overloading.ppt08 c++ Operator Overloading.ppt
08 c++ Operator Overloading.ppt
 
C++ overloading
C++ overloadingC++ overloading
C++ overloading
 
Operator overloading
Operator overloadingOperator overloading
Operator overloading
 
#OOP_D_ITS - 5th - C++ Oop Operator Overloading
#OOP_D_ITS - 5th - C++ Oop Operator Overloading#OOP_D_ITS - 5th - C++ Oop Operator Overloading
#OOP_D_ITS - 5th - C++ Oop Operator Overloading
 
Operator_Overloaing_Type_Conversion_OOPC(C++)
Operator_Overloaing_Type_Conversion_OOPC(C++)Operator_Overloaing_Type_Conversion_OOPC(C++)
Operator_Overloaing_Type_Conversion_OOPC(C++)
 
Operator overloading
Operator overloadingOperator overloading
Operator overloading
 
Operator overloading in C++
Operator overloading in C++Operator overloading in C++
Operator overloading in C++
 
operator overloading
operator overloadingoperator overloading
operator overloading
 

Similar to Operator overloadng

Lec 28 - operator overloading
Lec 28 - operator overloadingLec 28 - operator overloading
Lec 28 - operator overloading
Princess Sam
 

Similar to Operator overloadng (20)

OOPS-Seminar.pdf
OOPS-Seminar.pdfOOPS-Seminar.pdf
OOPS-Seminar.pdf
 
Operator overloaing
Operator overloaingOperator overloaing
Operator overloaing
 
Oops
OopsOops
Oops
 
Operator overloading (binary)
Operator overloading (binary)Operator overloading (binary)
Operator overloading (binary)
 
C++ - Constructors,Destructors, Operator overloading and Type conversion
C++ - Constructors,Destructors, Operator overloading and Type conversionC++ - Constructors,Destructors, Operator overloading and Type conversion
C++ - Constructors,Destructors, Operator overloading and Type conversion
 
OOP_UnitIII.pdf
OOP_UnitIII.pdfOOP_UnitIII.pdf
OOP_UnitIII.pdf
 
Operator overloading C++
Operator overloading C++Operator overloading C++
Operator overloading C++
 
B.sc CSIT 2nd semester C++ Unit4
B.sc CSIT  2nd semester C++ Unit4B.sc CSIT  2nd semester C++ Unit4
B.sc CSIT 2nd semester C++ Unit4
 
OperatorOverloading.ppt
OperatorOverloading.pptOperatorOverloading.ppt
OperatorOverloading.ppt
 
NIKUL SURANI
NIKUL SURANINIKUL SURANI
NIKUL SURANI
 
Operator Overloading
Operator OverloadingOperator Overloading
Operator Overloading
 
11.C++Polymorphism [Autosaved].pptx
11.C++Polymorphism [Autosaved].pptx11.C++Polymorphism [Autosaved].pptx
11.C++Polymorphism [Autosaved].pptx
 
Polymorphism
PolymorphismPolymorphism
Polymorphism
 
Lec 28 - operator overloading
Lec 28 - operator overloadingLec 28 - operator overloading
Lec 28 - operator overloading
 
Unit ii
Unit iiUnit ii
Unit ii
 
Cpp (C++)
Cpp (C++)Cpp (C++)
Cpp (C++)
 
Operator in c programming
Operator in c programmingOperator in c programming
Operator in c programming
 
3d7b7 session4 c++
3d7b7 session4 c++3d7b7 session4 c++
3d7b7 session4 c++
 
Operator overloading
Operator overloadingOperator overloading
Operator overloading
 
OCA JAVA - 3 Programming with Java Operators
 OCA JAVA - 3 Programming with Java Operators OCA JAVA - 3 Programming with Java Operators
OCA JAVA - 3 Programming with Java Operators
 

Operator overloadng

  • 2. Operator Overloading • Each C++ operator has a predefined meaning. Most of them are given additional meaning through the concept called operator overloading • Main idea behind Operator overloading is to use C++ operators with the class objects. Eg: ‘+’ can be used to add 2 ints, floats or doubles, the same ‘+’ can be used to add 2 class objects, thereby ‘+’ gets overloaded. • When an operator is overloaded, its original meaning is not lost.
  • 3. C++ provides a wide variety of operators to perform operations on various operands.
  • 4. return type class-name : :operator op (arg-list) { Function body // task defined } • return type is the type of value returned by the specified operation. • op is the operator being overloaded.It may be a unary or binary operator • op is preceded by the keyword operator. • operator op is the function name.
  • 5. The process of overloading involves the following 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. It may be either a member function or a friend function. – Define the operator function to implement the required operations.
  • 6. Operator Function must be either – member function (non-static) Or – friend function. The basic difference : • A friend function will have only one argument for unary operators and two for binary operators. • A member function has no arguments for unary operators and one argument for binary operators. • This is because the object used to invoke the member function is passed implicitly and therefore is available for the member function. • Arguments may be passed either by value or by reference.
  • 7. Overloaded operator functions can be invoked by expressions such as: For unary operators: op x or x op For binary operators: x op y op x or x op would be interpreted as for a friend function: operator op (x) for a member function: x.operator op ( ) x op y would be interpreted as for a friend function: operator op (x,y) for a member function: x.operator op (y)
  • 8. 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. – Overloaded operators follow the syntax rules of the original operators.
  • 9. Rules For Overloading Operators continue… – The following operators that cannot be overloaded: • Size of Size of operator • . Membership operator • .* Pointer-to-member operator • :: Scope resolution operator • ? ; Conditional operator
  • 10. Rules For Overloading Operators continue… – The following operators can be over loaded with the use of member functions and not by the use of friend functions: • Assignment operator = • Function call operator( ) • Subscripting operator [ ] • Class member access operator -> – 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 one reference argument.
  • 11. Rules For Overloading Operators continue… – Binary operators overloaded through a member function take one explicit argument and those which are overloaded through a friend function take two explicit arguments. – When using binary operators overloaded through a member function, the left hand operand must be an object of the relevant class. – Binary arithmetic operators such as +, -, * and / must explicitly return a value. They must not attempt to change their own arguments.