SlideShare a Scribd company logo
Chapter: 08

Operator Overloading
     Customised behaviour of operators

                Lecture: 28
              Date: 25.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 28 - 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
(!, & , ~ , …)   (++, --, …)
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;
}
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/
Class Inheritance

More Related Content

PPT
Lec 26.27-operator overloading
PPTX
Operator overloading
PPTX
Operator overloading and type conversions
PPT
14 operator overloading
PPTX
operator overloading
PPTX
Operator overloading and type conversion in cpp
PPT
Operator Overloading
PPTX
Operator overloading
Lec 26.27-operator overloading
Operator overloading
Operator overloading and type conversions
14 operator overloading
operator overloading
Operator overloading and type conversion in cpp
Operator Overloading
Operator overloading

What's hot (20)

PPT
Operator overloading
PPTX
Presentation on overloading
PPTX
Operator overloading
PPTX
operator overloading & type conversion in cpp over view || c++
PPTX
OPERATOR OVERLOADING IN C++
PPTX
PPT
Operator overloading
PPTX
Bca 2nd sem u-4 operator overloading
PPTX
Operator overloadng
PPT
08 c++ Operator Overloading.ppt
PDF
Operator_Overloaing_Type_Conversion_OOPC(C++)
PPTX
Operator overloading
PPTX
Unary operator overloading
PPT
Lecture5
PPT
C++ overloading
PPT
Operator overloading
PDF
Operator overloading
PPTX
PPTX
Operator overloading
PPTX
Data Type Conversion in C++
Operator overloading
Presentation on overloading
Operator overloading
operator overloading & type conversion in cpp over view || c++
OPERATOR OVERLOADING IN C++
Operator overloading
Bca 2nd sem u-4 operator overloading
Operator overloadng
08 c++ Operator Overloading.ppt
Operator_Overloaing_Type_Conversion_OOPC(C++)
Operator overloading
Unary operator overloading
Lecture5
C++ overloading
Operator overloading
Operator overloading
Operator overloading
Data Type Conversion in C++
Ad

Viewers also liked (11)

PPT
Unit3 C
PPTX
Classes function overloading
PPTX
Oo ps concepts in c++
PPT
Function Overlaoding
PPT
Oops And C++ Fundamentals
PPTX
c++ programming Unit 1 introduction to c++
PPTX
OOPS IN C++
PPTX
Function overloading
PPTX
Function overloading in c++
PPTX
Function overloading
PPT
Basics of c++ Programming Language
Unit3 C
Classes function overloading
Oo ps concepts in c++
Function Overlaoding
Oops And C++ Fundamentals
c++ programming Unit 1 introduction to c++
OOPS IN C++
Function overloading
Function overloading in c++
Function overloading
Basics of c++ Programming Language
Ad

Similar to Lec 28 - operator overloading (20)

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

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 28 - operator overloading

  • 1. Chapter: 08 Operator Overloading Customised behaviour of operators Lecture: 28 Date: 25.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.
  • 6. 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)
  • 7. 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)
  • 8. Operators that can be overloaded + - * / % ^ & | ~ ! = < > += -= *= /= %= ^= &= |= << >> >>= <<= == != <= >= && || ++ -- ->* , -> [] () new delete new[] delete[] Operators that cannot be overloaded . .* :: ?:
  • 9. Types of Operators OPERATORS Unary Binary (+, <, =, …) Prefix Postfix (!, & , ~ , …) (++, --, …)
  • 10. Unary Operators  Operators attached to a single operand, e.g., -a, +a, --a, a--, ++a, a++
  • 11. 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; }
  • 12. 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; }
  • 13. 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
  • 14. 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; }
  • 15. 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
  • 16. 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
  • 17. 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; }
  • 18. 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.
  • 19. 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() { }
  • 20. Conversion Between C-String and String Objects
  • 21. 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
  • 22. 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 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.
  • #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 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
  • #12: Student Book
  • #13: Student Book
  • #14: Student Book
  • #15: Student Book
  • #16: Student Book Exponential operator is reserved User-defined operators because of precedence problem
  • #17: Student Book
  • #18: Student Book
  • #19: Student Book
  • #20: Student Book
  • #21: Student Book
  • #22: Student Book
  • #23: Student Book
  • #24: Student Book