• We just launched and are currently in beta. Join us as we build and grow the community.

Object-Oriented Programming In C++: Operator Overloading

samaduske

Codebase Navigator
S Rep
0
0
0
Rep
0
S Vouches
0
0
0
Vouches
0
Posts
136
Likes
105
Bits
2 MONTHS
2 2 MONTHS OF SERVICE
LEVEL 1 300 XP
1. Introduction.

2. Basic Principles in Operator Overloading.

Introduction:

Calling function in some classes may be annoying, especially mathematical classes, for example:
  1. a =

    2

    +

    3
  2. assign(

    a, add(

    2

    ,3

    )

    )

    ;

In C++ the overloading principle applies not only to functions, but to operators too.
- Operators can be extended to work not just with built-in types but also classes.
- Operator is used on objects of that class.
- Operator Overloading makes it very easy to write code that feels natural.
- It requires special care, can be abused to make code unreadable.
Basic Principles in Operator Overloading:

When using operator overloading, you have to:
- use it to increase readability of your program.
- avoid exaggeration in use.
To use operator overloading, follow those steps:
1- write the definition of your function as usual.
2- your function name is consist of operator keyword Followed by the operation symbol which you overload.
For example to overloading addition operation (+) your function name will be :
operator+

It is possible to overloading almost all operators in C++. This list contains all operators we can overloading :

+
-
*
/
%
^
&
|

~
!
=

>
+=
-=
*=
/=
%=
^=
&=
|=

>>
>>=
=
==
!=
=
>=
&&
||
++
--
->*
,
->
[]
()
new
delete
new[]
delete[]

In the following example, we will overload and >> operators:
  1. // Overloading the stream-insertion and
  2. // stream-extraction operators.
  3. #include <iostream>

  4. using

    std::

    cout

    ;
  5. using

    std::

    cin

    ;
  6. using

    std::

    endl

    ;
  7. using

    std::

    ostream

    ;
  8. using

    std::

    istream

    ;

  9. #include <iomanip>

  10. using

    std::

    setw

    ;

  11. class

    PhoneNumber
  12. {
  13. friend

    ostream &

    operator<<

    (

    ostream&

    , const

    PhoneNumber&

    )

    ;
  14. friend

    istream &

    operator>>

    (

    istream&

    , PhoneNumber &

    )

    ;

  15. private

    :
  16. char

    areaCode[

    4

    ]

    ;

    // 3-digit area code and null
  17. char

    exchange[

    4

    ]

    ;

    // 3-digit exchange and null
  18. char

    line[

    5

    ]

    ;

    // 4-digit line and null
  19. }

    ;

  20. // Overloaded stream-insertion operator (cannot be
  21. // a member function if we would like to invoke it with
  22. //cout <<somePhoneNumber;).
  23. ostream &

    operator<<

    (

    ostream &

    output, constPhoneNumber &

    num )
  24. {
  25. output <<

    "("

    <<

    num.areaCode

    <<

    ") "
  26. <<

    num.exchange

    <<

    "-"

    <<

    num.line

    ;
  27. return

    output;

    // enables cout << a << b << c;
  28. }

  29. istream &

    operator>>

    (

    istream &

    input, PhoneNumber &

    num )
  30. {
  31. input.ignore

    (

    )

    ;

    // skip (
  32. input >>

    setw(

    4

    )

    >>

    num.areaCode

    ;

    // input area code
  33. input.ignore

    (

    2

    )

    ;

    // skip ) and space
  34. input >>

    setw(

    4

    )

    >>

    num.exchange

    ;

    // input exchange
  35. input.ignore

    (

    )

    ;

    // skip dash (-)
  36. input >>

    setw(

    5

    )

    >>

    num.line

    ;

    // input line
  37. return

    input;

    // enables cin >> a >> b >> c;
  38. }

  39. int

    main(

    )
  40. {
  41. PhoneNumber phone;

    // create object phone

  42. cout

    <<

    "Enter phone number in the form (123) 456-7890:\n

    "

    ;

  43. // cin >> phone invokes operator>> function by
  44. // issuing the call operator>>(cin, phone ).
  45. cin

    >>

    phone;

  46. // cout<< phone invokes operator<< function by
  47. // issuing the call operator<<(cout, phone ).
  48. cout

    <<

    "The phone number entered was: "

    <<

    phone <<

    endl;
  49. return

    0

    ;
  50. }

The Output:

Enter phone number in the form (123) 456-7890:
(800) 555-1212
The phone number entered was: (800) 555-1212

Note: You can find the full source code of this example in code.zip file.


Download
You must upgrade your account or reply in the thread to view hidden text.
 

450,270

322,965

322,974

Top