SlideShare una empresa de Scribd logo
1 de 23
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.
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

Más contenido relacionado

La actualidad más candente

Operator overloading
Operator overloadingOperator overloading
Operator overloadingArunaDevi63
 
Presentation on overloading
Presentation on overloading Presentation on overloading
Presentation on overloading Charndeep Sekhon
 
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++gourav kottawar
 
OPERATOR OVERLOADING IN C++
OPERATOR OVERLOADING IN C++OPERATOR OVERLOADING IN C++
OPERATOR OVERLOADING IN C++Aabha Tiwari
 
Operator overloading
Operator overloadingOperator overloading
Operator overloadingabhay singh
 
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 overloadingRai University
 
Operator overloadng
Operator overloadngOperator overloadng
Operator overloadngpreethalal
 
08 c++ Operator Overloading.ppt
08 c++ Operator Overloading.ppt08 c++ Operator Overloading.ppt
08 c++ Operator Overloading.pptTareq Hasan
 
Operator_Overloaing_Type_Conversion_OOPC(C++)
Operator_Overloaing_Type_Conversion_OOPC(C++)Operator_Overloaing_Type_Conversion_OOPC(C++)
Operator_Overloaing_Type_Conversion_OOPC(C++)Yaksh Jethva
 
Operator overloading
Operator overloadingOperator overloading
Operator overloadingBurhan Ahmed
 
C++ overloading
C++ overloadingC++ overloading
C++ overloadingsanya6900
 
Operator overloading
Operator overloadingOperator overloading
Operator overloadingKamal Acharya
 
Data Type Conversion in C++
Data Type Conversion in C++Data Type Conversion in C++
Data Type Conversion in C++Danial Mirza
 

La actualidad más candente (20)

Operator overloading
Operator overloadingOperator overloading
Operator overloading
 
Presentation on overloading
Presentation on overloading Presentation on overloading
Presentation on overloading
 
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 IN C++
OPERATOR OVERLOADING IN C++OPERATOR OVERLOADING IN C++
OPERATOR OVERLOADING IN C++
 
operator overloading in C++
operator overloading in C++operator overloading in C++
operator overloading in C++
 
Operator overloading
Operator overloadingOperator overloading
Operator overloading
 
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 overloadng
Operator overloadngOperator overloadng
Operator overloadng
 
08 c++ Operator Overloading.ppt
08 c++ Operator Overloading.ppt08 c++ Operator Overloading.ppt
08 c++ Operator Overloading.ppt
 
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
 
Unary operator overloading
Unary operator overloadingUnary operator overloading
Unary operator overloading
 
Lecture5
Lecture5Lecture5
Lecture5
 
C++ overloading
C++ overloadingC++ overloading
C++ overloading
 
Operator overloading
Operator overloading Operator overloading
Operator overloading
 
Operator overloading
Operator overloadingOperator overloading
Operator overloading
 
Oops
OopsOops
Oops
 
Operator overloading
Operator overloadingOperator overloading
Operator overloading
 
Data Type Conversion in C++
Data Type Conversion in C++Data Type Conversion in C++
Data Type Conversion in C++
 

Destacado

Classes function overloading
Classes function overloadingClasses function overloading
Classes function overloadingankush_kumar
 
Oo ps concepts in c++
Oo ps concepts in c++Oo ps concepts in c++
Oo ps concepts in c++Hemant Saini
 
Oops And C++ Fundamentals
Oops And C++ FundamentalsOops And C++ Fundamentals
Oops And C++ FundamentalsSubhasis Nayak
 
c++ programming Unit 1 introduction to c++
c++ programming Unit 1  introduction to c++c++ programming Unit 1  introduction to c++
c++ programming Unit 1 introduction to c++AAKASH KUMAR
 
Function overloading in c++
Function overloading in c++Function overloading in c++
Function overloading in c++Learn By Watch
 
Basics of c++ Programming Language
Basics of c++ Programming LanguageBasics of c++ Programming Language
Basics of c++ Programming LanguageAhmad Idrees
 

Destacado (11)

Unit3 C
Unit3 C Unit3 C
Unit3 C
 
Classes function overloading
Classes function overloadingClasses function overloading
Classes function overloading
 
Oo ps concepts in c++
Oo ps concepts in c++Oo ps concepts in c++
Oo ps concepts in c++
 
Function Overlaoding
Function OverlaodingFunction Overlaoding
Function Overlaoding
 
Oops And C++ Fundamentals
Oops And C++ FundamentalsOops And C++ Fundamentals
Oops And C++ Fundamentals
 
c++ programming Unit 1 introduction to c++
c++ programming Unit 1  introduction to c++c++ programming Unit 1  introduction to c++
c++ programming Unit 1 introduction to c++
 
OOPS IN C++
OOPS IN C++OOPS IN C++
OOPS IN C++
 
Function overloading
Function overloadingFunction overloading
Function overloading
 
Function overloading in c++
Function overloading in c++Function overloading in c++
Function overloading in c++
 
Function overloading
Function overloadingFunction overloading
Function overloading
 
Basics of c++ Programming Language
Basics of c++ Programming LanguageBasics of c++ Programming Language
Basics of c++ Programming Language
 

Similar a Lec 28 - operator overloading

Similar a Lec 28 - operator overloading (20)

Operator overloaing
Operator overloaingOperator overloaing
Operator overloaing
 
Ch-4-Operator Overloading.pdf
Ch-4-Operator Overloading.pdfCh-4-Operator Overloading.pdf
Ch-4-Operator Overloading.pdf
 
3d7b7 session4 c++
3d7b7 session4 c++3d7b7 session4 c++
3d7b7 session4 c++
 
overloading in C++
overloading in C++overloading in C++
overloading in C++
 
Operator overloading
Operator overloadingOperator overloading
Operator overloading
 
Operator Overloading and Scope of Variable
Operator Overloading and Scope of VariableOperator Overloading and Scope of Variable
Operator Overloading and Scope of Variable
 
Overloading
OverloadingOverloading
Overloading
 
OOPS-Seminar.pdf
OOPS-Seminar.pdfOOPS-Seminar.pdf
OOPS-Seminar.pdf
 
NIKUL SURANI
NIKUL SURANINIKUL SURANI
NIKUL SURANI
 
08 c-operator-overloadingppt2563
08 c-operator-overloadingppt256308 c-operator-overloadingppt2563
08 c-operator-overloadingppt2563
 
Operator Overloading
Operator OverloadingOperator Overloading
Operator Overloading
 
Cpp (C++)
Cpp (C++)Cpp (C++)
Cpp (C++)
 
Mca 2nd sem u-4 operator overloading
Mca 2nd  sem u-4 operator overloadingMca 2nd  sem u-4 operator overloading
Mca 2nd sem u-4 operator overloading
 
Object Oriented Programming using C++ - Part 3
Object Oriented Programming using C++ - Part 3Object Oriented Programming using C++ - Part 3
Object Oriented Programming using C++ - Part 3
 
Operator overloading (binary)
Operator overloading (binary)Operator overloading (binary)
Operator overloading (binary)
 
operator overloading
operator overloadingoperator overloading
operator overloading
 
Operator oveerloading
Operator oveerloadingOperator oveerloading
Operator oveerloading
 
Operator overloading C++
Operator overloading C++Operator overloading C++
Operator overloading C++
 
OPERATING OVERLOADING IN VHDL
OPERATING OVERLOADING IN VHDLOPERATING OVERLOADING IN VHDL
OPERATING OVERLOADING IN VHDL
 
Chapter 11 Function
Chapter 11 FunctionChapter 11 Function
Chapter 11 Function
 

Más de Princess Sam

Lec 49 - stream-files
Lec 49 - stream-filesLec 49 - stream-files
Lec 49 - stream-filesPrincess Sam
 
Lec 42.43 - virtual.functions
Lec 42.43 - virtual.functionsLec 42.43 - virtual.functions
Lec 42.43 - virtual.functionsPrincess Sam
 
Lec 40.41 - pointers
Lec 40.41 -  pointersLec 40.41 -  pointers
Lec 40.41 - pointersPrincess Sam
 
Lec 38.39 - pointers
Lec 38.39 -  pointersLec 38.39 -  pointers
Lec 38.39 - pointersPrincess Sam
 
Lec 47.48 - stream-files
Lec 47.48 - stream-filesLec 47.48 - stream-files
Lec 47.48 - stream-filesPrincess Sam
 
Lec 45.46- virtual.functions
Lec 45.46- virtual.functionsLec 45.46- virtual.functions
Lec 45.46- virtual.functionsPrincess Sam
 
Lec 33 - inheritance
Lec 33 -  inheritanceLec 33 -  inheritance
Lec 33 - inheritancePrincess Sam
 
Lec 30.31 - inheritance
Lec 30.31 -  inheritanceLec 30.31 -  inheritance
Lec 30.31 - inheritancePrincess Sam
 
Lec 25 - arrays-strings
Lec 25 - arrays-stringsLec 25 - arrays-strings
Lec 25 - arrays-stringsPrincess Sam
 

Más de Princess Sam (12)

Lec 50
Lec 50Lec 50
Lec 50
 
Lec 49 - stream-files
Lec 49 - stream-filesLec 49 - stream-files
Lec 49 - stream-files
 
Lec 42.43 - virtual.functions
Lec 42.43 - virtual.functionsLec 42.43 - virtual.functions
Lec 42.43 - virtual.functions
 
Lec 40.41 - pointers
Lec 40.41 -  pointersLec 40.41 -  pointers
Lec 40.41 - pointers
 
Lec 38.39 - pointers
Lec 38.39 -  pointersLec 38.39 -  pointers
Lec 38.39 - pointers
 
Lec 47.48 - stream-files
Lec 47.48 - stream-filesLec 47.48 - stream-files
Lec 47.48 - stream-files
 
Lec 45.46- virtual.functions
Lec 45.46- virtual.functionsLec 45.46- virtual.functions
Lec 45.46- virtual.functions
 
Lec 37 - pointers
Lec 37 -  pointersLec 37 -  pointers
Lec 37 - pointers
 
Lec 33 - inheritance
Lec 33 -  inheritanceLec 33 -  inheritance
Lec 33 - inheritance
 
Lec 30.31 - inheritance
Lec 30.31 -  inheritanceLec 30.31 -  inheritance
Lec 30.31 - inheritance
 
Lec 25 - arrays-strings
Lec 25 - arrays-stringsLec 25 - arrays-strings
Lec 25 - arrays-strings
 
Lec 36 - pointers
Lec 36 -  pointersLec 36 -  pointers
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.
  • 5.
  • 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/

Notas del editor

  1. Student Book
  2. Student Book
  3. Student Book
  4. Student Book
  5. Student Book
  6. 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.
  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 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
  11. Student Book
  12. Student Book
  13. Student Book
  14. Student Book
  15. Student Book Exponential operator is reserved User-defined operators because of precedence problem
  16. Student Book
  17. Student Book
  18. Student Book
  19. Student Book
  20. Student Book
  21. Student Book
  22. Student Book
  23. Student Book