SlideShare a Scribd company logo
Chapter: 08

Operator Overloading
     Customised behaviour of operators

              Lecture: 26 & 27
              Date: 24.09.2012
Objectives
   Overloading in C++
     Function overloading
     Operator overloading

   Different types of operators and their overloading
   Operators that cannot be overloaded
   Data conversion
     Automatic type conversion
     User-defined type conversion
C++ Overloading
   Overloading in C++ allows to specify more than one
    definition for a function name or an operator in the same
    scope, which is called function overloading and operator
    overloading respectively.

                      C++ OVERLAODING



                Function                Operator

   An overloaded declaration is the one that had been declared
    with exactly the same name as the previous declaration in
    the same scope, except that both declarations have different
    arguments and also different definition (implementation).
C++ Function Overloading
   An overloaded function can have multiple definitions for
    the same function name in the same scope.

   The definition of the function must differ from each other
    by the types and/or the number of arguments in the
    argument list.

   Function declarations cannot be overloaded if they differ
    only by return type.
Lec 26.27-operator overloading
Lec 26.27-operator overloading
C++ Operator Overloading
   It simplifies the program listing, e.g.,
    d3.addobjects(d1, d2)
or the similar but equally obscure
    d3 = d1.addobjects(d2)
can be changed to much more readable form
    d3 = d1 + d2
   Operator overloading refers to giving normal C++
    operators such as +, *, and <= so on, an additional
    meaning when they are applied to user defined data types,
    e.g.,
        d3 = d1 + d2 (legal when d1, d2, and d3 are basic types)
C++ Operator Overloading (Syntax)
returnType operator*(parameters);
    ↑         ↑    ↑
    any type        keyword operator symbol


   Return type may be whatever the operator returns
        Including a reference to the object of the operand

   Operator symbol may be any valid operator allowed
    by the language compiler (see the following list)
Operators that can be overloaded
     +         -      *           /       %         ^

    &          |      ~           !       =         <

     >         +=    -=          *=       /=      %=

    ^=         &=    |=          <<      >>       >>=

    <<=        ==     !=         <=      >=       &&

    ||         ++     --         ->*       ,       ->

     []        ()    new        delete   new[]   delete[]




Operators that cannot be overloaded

.              .*          ::            ?:
Types of Operators

                 OPERATORS


            Unary            Binary
                           (+, <, =, …)

    Prefix         Postfix
(!, & , ~ , …)   (++, --, …)
Example: Operator Overloading
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);   }
};
int main()
{    OverloadingExample object1(10);
     cout << object1 + 10; // overloaded operator called

     getch();
     return 0;
}
Unary Operators


   Operators attached to a single operand,
     e.g., -a, +a, --a, a--, ++a, a++
Example: Unary Operators (Prefix)
class UnaryExample
{    private:
        int m_LocalInt;
     public:
        UnaryExample(int j)
        {      m_LocalInt = j;   }


        int operator++ ()
        {      return (++m_LocalInt);   }
};
int main()
{    UnaryExample object1(10);
    cout << ++object1; // overloaded operator
getch();
return 0;
}
Example: Unary Operators (Postfix)
class UnaryExample
{    private:
        int m_LocalInt;
     public:
        UnaryExample(int j)
        {      m_LocalInt = j;   }


        int operator++ (int)     // “int” argument for postfix operator
        {      return m_LocalInt++; }
};
int main()
{    UnaryExample object1(10);
    cout << object1++; // overloaded operator
getch();
return 0;
}
Lec 26.27-operator overloading
Lec 26.27-operator overloading
Binary Operators

   Operators attached to two operands,
    e.g.,
    a-b, a+b, a*b, a/b, a%b, a>b, a>=b,
          a<b, a<=b, a==b
Example: Binary Operators
class BinaryExample
{
     private:
        int m_LocalInt;
     public:
        BinaryExample(int j)
            {     m_LocalInt = j;   }


        int operator+ (BinaryExample& rhsObj)
        {       return (m_LocalInt + rhsObj.m_LocalInt);   }
};
int main()
{    BinaryExample object1(10), object2(20);
    cout << object1 + object2; // overloaded operator called
getch();
return 0;
}
Non-Overloadable Operators
   Operators that cannot be overloaded due to
    safety reasons:
     Member Selection ‘.’ operator
     Member dereference ‘.*’ operator

     Exponential ‘**’ operator

     User-defined operators

     Operator precedence rules
Data Conversion
   Assignment operator assigns a value from one side to
    another, e.g.,
               intvar1 = intvar2
    But what happens when the variables on different
    sides of the = sign are of different types?
   Two possibilities:
     Automatic data conversion
     User-defined data conversion
Conversion Between basic Types
#include<iostream>
#include<conio.h>
using namespace std;

int main()
{
int intvar;
float floatvar;

intvar = static_cast<int>(floatvar);   //casting provides explicit conversion



getch();
return 0;
}
Conversion between User-defined
            and Basic Types

   Built-in conversion routines can’t be relied while
    converting b/w user-defined data types and basic
    types; since the compiler doesn’t know anything
    about user-defined types besides what we tell it.
Conversion between User-defined
            and Basic Types
   Create a member function that takes the current type
   Converts it to the desired type using the operator
    keyword followed by the type you want to convert to.
   Return type is the name of the operator overloaded
   Reflexivity - global overloading instead of member
    overloading; for code saving.
   Syntax:
              operator type_name()
              {            }
Conversion Between C-String and String
Objects
Lecture Summary
    Lecture covered …
   Overloading in C++
     Function overloading
     Operator overloading

   Different types of operator
   Operators that cannot be overloaded
   Data conversion:
     Automatic type conversion
     User-defined type conversion
Lecture Summary

Lectures, books and so on will be updated at:

           http://www.itquest.tk/
       (http://www.itquest.ucoz.com/)

http://www.downloadbooks.mytestproject.co.cc/

More Related Content

PPTX
OPERATOR OVERLOADING IN C++
PPTX
Operator overloading
PPTX
Operator overloading
PPTX
Operator overloading
PPTX
PPTX
Operator overloadng
PPTX
Operator overloading and type conversion in cpp
PPTX
Operator overloading and type conversions
OPERATOR OVERLOADING IN C++
Operator overloading
Operator overloading
Operator overloading
Operator overloadng
Operator overloading and type conversion in cpp
Operator overloading and type conversions

What's hot (20)

PPT
Operator overloading
PDF
Operator overloading
PPTX
Presentation on overloading
PPTX
Bca 2nd sem u-4 operator overloading
PPTX
operator overloading
PPT
Operator Overloading
PPTX
operator overloading & type conversion in cpp over view || c++
PPTX
Unary operator overloading
PPTX
Operator overloading
PPT
Operator overloading
PPT
14 operator overloading
PPT
Operator overloading in C++
PPTX
Operator overloading
PPT
08 c++ Operator Overloading.ppt
PPT
C++ overloading
PPT
Operator overloading
PPT
Lec 28 - operator overloading
PPTX
#OOP_D_ITS - 5th - C++ Oop Operator Overloading
PDF
Operator_Overloaing_Type_Conversion_OOPC(C++)
Operator overloading
Operator overloading
Presentation on overloading
Bca 2nd sem u-4 operator overloading
operator overloading
Operator Overloading
operator overloading & type conversion in cpp over view || c++
Unary operator overloading
Operator overloading
Operator overloading
14 operator overloading
Operator overloading in C++
Operator overloading
08 c++ Operator Overloading.ppt
C++ overloading
Operator overloading
Lec 28 - operator overloading
#OOP_D_ITS - 5th - C++ Oop Operator Overloading
Operator_Overloaing_Type_Conversion_OOPC(C++)
Ad

Viewers also liked (19)

PDF
06. operator overloading
PPTX
operator overloading in c++
PPTX
Function overloading
PPT
Operator Overloading
PPT
098ca session7 c++
PPTX
Intro To C++ - Class 14 - Midterm Review
PDF
Operator overloading in C++
PPT
Fp201 unit2 1
PDF
C++ Object oriented concepts & programming
PPTX
C++ And Object in lecture3
PPTX
class c++
PPSX
Complete C++ programming Language Course
PPT
Set Theory In C++
PPT
Structure of C++ - R.D.Sivakumar
PDF
Practicle application of maxima and minima
PDF
Pointer in c++ part1
PPTX
Object Oriented Programming Using C++
PPT
Control structure C++
06. operator overloading
operator overloading in c++
Function overloading
Operator Overloading
098ca session7 c++
Intro To C++ - Class 14 - Midterm Review
Operator overloading in C++
Fp201 unit2 1
C++ Object oriented concepts & programming
C++ And Object in lecture3
class c++
Complete C++ programming Language Course
Set Theory In C++
Structure of C++ - R.D.Sivakumar
Practicle application of maxima and minima
Pointer in c++ part1
Object Oriented Programming Using C++
Control structure C++
Ad

Similar to Lec 26.27-operator overloading (20)

PPTX
PDF
Ch-4-Operator Overloading.pdf
PPTX
Operator overloaing
PDF
overloading in C++
PDF
Operator overloading
PPT
3d7b7 session4 c++
PDF
22 scheme OOPs with C++ BCS306B_module3.pdf
PPTX
Operator Overloading and Scope of Variable
PDF
Basics _of_Operator Overloading_Somesh_Kumar_SSTC
PPTX
operator overloading
PPTX
Mca 2nd sem u-4 operator overloading
PPTX
Object Oriented Programming using C++: Ch08 Operator Overloading.pptx
PPT
Overloading
PDF
Polymorphism and Type Conversion.pdf pot
PPT
Polymorphism and function overloading_new.ppt
PPTX
Functions in C++ programming language.pptx
PPTX
Operator overloading2
PPT
08 c-operator-overloadingppt2563
PDF
OOPS-Seminar.pdf
PDF
Object Oriented Programming using C++ - Part 3
Ch-4-Operator Overloading.pdf
Operator overloaing
overloading in C++
Operator overloading
3d7b7 session4 c++
22 scheme OOPs with C++ BCS306B_module3.pdf
Operator Overloading and Scope of Variable
Basics _of_Operator Overloading_Somesh_Kumar_SSTC
operator overloading
Mca 2nd sem u-4 operator overloading
Object Oriented Programming using C++: Ch08 Operator Overloading.pptx
Overloading
Polymorphism and Type Conversion.pdf pot
Polymorphism and function overloading_new.ppt
Functions in C++ programming language.pptx
Operator overloading2
08 c-operator-overloadingppt2563
OOPS-Seminar.pdf
Object Oriented Programming using C++ - Part 3

More from Princess Sam (12)

PPT
Lec 50
PPT
Lec 49 - stream-files
PPT
Lec 42.43 - virtual.functions
PPT
Lec 40.41 - pointers
PPT
Lec 38.39 - pointers
PPT
Lec 47.48 - stream-files
PPT
Lec 45.46- virtual.functions
PPT
Lec 37 - pointers
PPT
Lec 33 - inheritance
PPT
Lec 30.31 - inheritance
PPT
Lec 25 - arrays-strings
PPT
Lec 36 - pointers
Lec 50
Lec 49 - stream-files
Lec 42.43 - virtual.functions
Lec 40.41 - pointers
Lec 38.39 - pointers
Lec 47.48 - stream-files
Lec 45.46- virtual.functions
Lec 37 - pointers
Lec 33 - inheritance
Lec 30.31 - inheritance
Lec 25 - arrays-strings
Lec 36 - pointers

Lec 26.27-operator overloading

  • 1. Chapter: 08 Operator Overloading Customised behaviour of operators Lecture: 26 & 27 Date: 24.09.2012
  • 2. Objectives  Overloading in C++  Function overloading  Operator overloading  Different types of operators and their overloading  Operators that cannot be overloaded  Data conversion  Automatic type conversion  User-defined type conversion
  • 3. C++ Overloading  Overloading in C++ allows to specify more than one definition for a function name or an operator in the same scope, which is called function overloading and operator overloading respectively. C++ OVERLAODING Function Operator  An overloaded declaration is the one that had been declared with exactly the same name as the previous declaration in the same scope, except that both declarations have different arguments and also different definition (implementation).
  • 4. C++ Function Overloading  An overloaded function can have multiple definitions for the same function name in the same scope.  The definition of the function must differ from each other by the types and/or the number of arguments in the argument list.  Function declarations cannot be overloaded if they differ only by return type.
  • 7. C++ Operator Overloading  It simplifies the program listing, e.g., d3.addobjects(d1, d2) or the similar but equally obscure d3 = d1.addobjects(d2) can be changed to much more readable form d3 = d1 + d2  Operator overloading refers to giving normal C++ operators such as +, *, and <= so on, an additional meaning when they are applied to user defined data types, e.g., d3 = d1 + d2 (legal when d1, d2, and d3 are basic types)
  • 8. C++ Operator Overloading (Syntax) returnType operator*(parameters); ↑ ↑ ↑ any type keyword operator symbol  Return type may be whatever the operator returns  Including a reference to the object of the operand  Operator symbol may be any valid operator allowed by the language compiler (see the following list)
  • 9. Operators that can be overloaded + - * / % ^ & | ~ ! = < > += -= *= /= %= ^= &= |= << >> >>= <<= == != <= >= && || ++ -- ->* , -> [] () new delete new[] delete[] Operators that cannot be overloaded . .* :: ?:
  • 10. Types of Operators OPERATORS Unary Binary (+, <, =, …) Prefix Postfix (!, & , ~ , …) (++, --, …)
  • 11. Example: Operator Overloading 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); } }; int main() { OverloadingExample object1(10); cout << object1 + 10; // overloaded operator called getch(); return 0; }
  • 12. Unary Operators  Operators attached to a single operand, e.g., -a, +a, --a, a--, ++a, a++
  • 13. Example: Unary Operators (Prefix) class UnaryExample { private: int m_LocalInt; public: UnaryExample(int j) { m_LocalInt = j; } int operator++ () { return (++m_LocalInt); } }; int main() { UnaryExample object1(10); cout << ++object1; // overloaded operator getch(); return 0; }
  • 14. Example: Unary Operators (Postfix) class UnaryExample { private: int m_LocalInt; public: UnaryExample(int j) { m_LocalInt = j; } int operator++ (int) // “int” argument for postfix operator { return m_LocalInt++; } }; int main() { UnaryExample object1(10); cout << object1++; // overloaded operator getch(); return 0; }
  • 17. Binary Operators  Operators attached to two operands, e.g., a-b, a+b, a*b, a/b, a%b, a>b, a>=b, a<b, a<=b, a==b
  • 18. Example: Binary Operators class BinaryExample { private: int m_LocalInt; public: BinaryExample(int j) { m_LocalInt = j; } int operator+ (BinaryExample& rhsObj) { return (m_LocalInt + rhsObj.m_LocalInt); } }; int main() { BinaryExample object1(10), object2(20); cout << object1 + object2; // overloaded operator called getch(); return 0; }
  • 19. Non-Overloadable Operators  Operators that cannot be overloaded due to safety reasons:  Member Selection ‘.’ operator  Member dereference ‘.*’ operator  Exponential ‘**’ operator  User-defined operators  Operator precedence rules
  • 20. Data Conversion  Assignment operator assigns a value from one side to another, e.g., intvar1 = intvar2  But what happens when the variables on different sides of the = sign are of different types?  Two possibilities:  Automatic data conversion  User-defined data conversion
  • 21. Conversion Between basic Types #include<iostream> #include<conio.h> using namespace std; int main() { int intvar; float floatvar; intvar = static_cast<int>(floatvar); //casting provides explicit conversion getch(); return 0; }
  • 22. Conversion between User-defined and Basic Types  Built-in conversion routines can’t be relied while converting b/w user-defined data types and basic types; since the compiler doesn’t know anything about user-defined types besides what we tell it.
  • 23. Conversion between User-defined and Basic Types  Create a member function that takes the current type  Converts it to the desired type using the operator keyword followed by the type you want to convert to.  Return type is the name of the operator overloaded  Reflexivity - global overloading instead of member overloading; for code saving.  Syntax: operator type_name() { }
  • 24. Conversion Between C-String and String Objects
  • 25. Lecture Summary Lecture covered …  Overloading in C++  Function overloading  Operator overloading  Different types of operator  Operators that cannot be overloaded  Data conversion:  Automatic type conversion  User-defined type conversion
  • 26. Lecture Summary Lectures, books and so on will be updated at: http://www.itquest.tk/ (http://www.itquest.ucoz.com/) http://www.downloadbooks.mytestproject.co.cc/

Editor's Notes

  • #2: Student Book
  • #3: Student Book
  • #4: Student Book
  • #5: Student Book
  • #6: Student Book
  • #7: Student Book
  • #8: Student Book Operator overloading is used for customised functionality of a an operator in a class. This a powerful tool in C++, where hundreds of lines of code can be slashed, if operator overloaded it done properly and efficiently.
  • #9: Student Book Operator overloading is used for customised functionality of a an operator in a class. This a powerful tool in C++, where hundreds of lines of code can be slashed, if operator overloaded it done properly and efficiently.
  • #10: Student Book Operator overloading is used for customised functionality of a an operator in a class. This a powerful tool in C++, where hundreds of lines of code can be slashed, if operator overloaded it done properly and efficiently.
  • #11: Student Book Operator overloading is used for customised functionality of a an operator in a class. This a powerful tool in C++, where hundreds of lines of code can be slashed, if operator overloaded it done properly and efficiently.
  • #12: Student Book This example uses the ‘+’ overloaded operator for class level addition operations.
  • #13: Student Book The pre and post increment and decrement operators and overloading in different ways. This is because they have a different effect on objects and their values. e.g. a=14; cout &lt;&lt; a++; // will print 14 and increment a cout &lt;&lt; ++a; // will increment a and print 15
  • #14: Student Book
  • #15: Student Book
  • #16: Student Book
  • #17: Student Book
  • #18: Student Book
  • #19: Student Book
  • #20: Student Book Exponential operator is reserved User-defined operators because of precedence problem
  • #21: Student Book
  • #22: Student Book
  • #23: Student Book
  • #24: Student Book
  • #25: Student Book
  • #26: Student Book
  • #27: Student Book