Object-Oriented Programming In C++: Operator Overloading

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.  
  5. using std::cout;
  6. using std::cin;
  7. using std::endl;
  8. using std::ostream;
  9. using std::istream;
  10.  
  11. #include <iomanip>
  12.  
  13. using std::setw;
  14.  
  15. class PhoneNumber
  16. {
  17.    friend ostream &operator<<( ostream&, const PhoneNumber& );
  18.    friend istream &operator>>( istream&, PhoneNumber &);
  19.  
  20.    private:
  21.      char areaCode[ 4 ];  // 3-digit area code and null
  22.      char exchange[ 4 ];  // 3-digit exchange and null
  23.      char line[ 5 ];  // 4-digit line and null
  24. };
  25.  
  26. // Overloaded stream-insertion operator (cannot be
  27. // a member function if we would like to invoke it with
  28. //cout <<somePhoneNumber;).
  29. ostream &operator<<( ostream &output, constPhoneNumber &num )
  30. {
  31.      output << "(" << num.areaCode << ") "
  32.      << num.exchange << "-" << num.line;
  33.      return output;  // enables cout << a << b << c;
  34. }
  35.  
  36. istream &operator>>( istream &input, PhoneNumber &num )
  37. {
  38.    input.ignore();  // skip (
  39.    input >> setw( 4 ) >> num.areaCode; // input area code
  40.    input.ignore( 2 );  // skip ) and space
  41.    input >> setw( 4 ) >> num.exchange; // input exchange
  42.    input.ignore();  // skip dash (-)
  43.    input >>setw( 5 ) >> num.line;  // input line
  44.    return input;  // enables cin >> a >> b >> c;
  45. }
  46.  
  47. int main()
  48. {
  49.    PhoneNumber phone; // create object phone
  50.  
  51.    cout << "Enter phone number in the form (123) 456-7890:\n";
  52.  
  53.    // cin >> phone invokes operator>> function by
  54.    // issuing the call operator>>(cin, phone ).
  55.    cin >> phone;
  56.  
  57.    // cout<< phone invokes operator<< function by
  58.    // issuing the call operator<<(cout, phone ).
  59.    cout << "The phone number entered was: " << phone << endl;
  60.    return 0;
  61. }
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.
Tags

Add new comment