��������� ���� C++
���� 39
.
������������ (�������������) �� ��������� ����������� ���������
(Overloading Binary Arithmetic Operators)
.
(����������)
.
������������ �� ��������� (Operator Overloading). ������, � ������ 25 �� ���������� � �.�. ����������� �������, ����� ���� ���� � ���� ��� � ������� � �������� �� ��� ���������, � ����� ������� ��������� ���������� �� �� �����, ����� ������ �� ���� �� ����������� ��. ������� �� ���, ������������� ��������� ���������� �������� ��� ��������������, ���� �� �������� ���� � ��� �������� � �������� �� ��� ��������, ����� �������� �������� �������� ��������, � ���������� �� ���� �� ���� ��������.
.
! ������� �� ���������� � ������� �� Operator Overloading. ������� �� ����� �� �� ������� ���� ������������ �� ���������, ������ �� ������� ���������� �� ����������� (�.�. �� ������ �� ���������). �� ������ ��� ���������� ������� ������������� ������ (built-in types), � ������� �� �� ��� ���������� ������������ ���������, �� �.�. built-in operators, �� ������������ �� ������������ �� ��������� �� ����� �� ���� ����� ���������. ������ �� �������� � ������������� ������� (Overloaded Functions) �� ���������� ������� ������������ �� ���������.

� ����� �� ������, ��������� ����������� ��������� �� ������ � ������� ���� [1], �� �� ��������� � �������������� �� �����������. ���� � ���� ����� ����� ��������� �� ���������� ���� C++, ����� ����� ������ ���������� �� �������� ��������� �� �������� � ���������� �� ����������� ������.

����� �� �������� � �������������� �� ��������� ����������� ��������� +, -, *, � /. � ���������� ������ �� ���������� �������������� �� ������� ������� ���������, ���� ����������� �� ��������� <, >, <=, >=, ==!=, ����� � ���� �� �����������: +=, -=, *=/=. ���� ���� �� ������������� ����������� ����� �������� ��������� ++, --- (������ �����).

������������ �� ������ (�����) �� ���� ��� � ���� (data conversion) � ����� �������� � �������������� �� ��������� � ����� �� ����� � ������ ����� �� �� ������ �� ���������� ������������ �� ��� ������ � ��������������� ������ (���� int, float � �.�.), ����� � ������������ �� ������ �� ���� ��������� �� ��� ���� � ������ �� ���� ����.

������ � ���� ����� �� �������� � �������������� �� ��������� �� ����������� = � �������� ����� [] (subscript operator, ����� �� ��������� �� �������� ��� ����� ������� �� �����). �� ����� ������������ ������������� �������, ����� ������ �������������� �� ��������� ��-��������� � ��-���������.

� ������� �� ����� ���������� ������ �������� �� ������������ ���� �� ������ [1] (����� ������ ���� � ������� ��������� � ������), �� ���� ���� �� ����� ��������� � ��������� � ������ ��������� ���������� �� ������ � ���������� ����� - ����� �������� ������ 10������ 11.
.
1. ���� �� ����������� ���������? �������� �� �� ����� xString, ����� ���������� �� �������� ��������� (������) - ����� �. 6 �� ������ 19. � ���� ��� ������� �� ��������� ���� � ���� (concatenate) � ������� �� ��������� void append(xString xs), ����� �������� ������� � ������ xs ��� ������� �� ������, ����� ������� ���������. ��������,

 s1.append(s2);  // append s2 to s1

�� ������ ������� �� ������ s2 ��� ���� �� s1.

���� ����� ������� ����� �� ���� ���������� �� ������� � "�������" �� ���� �������, �.�. �� �������, ��� ������ ���� �� �������� (������ ��� ����� �� ������) � ��� ������ ��������� � ��� �� �������. ���� ���� ���� ����� �� � ���������, ����� � �������� ���������

  // you can append two strings
  s1 = s2 + s1;

�� ���������� cppstr.cpp �� �. 5 �� ������ ������ 19, ����� �������� ��������� ����� string �� ������������ C++ ���������� (�������� � #include <string>).

�������� �� � �� ����� complex_number �� ���������� complex2.cpp �� ������ ������ 11, ����� �������� ������������ �����. � ���, �� �� �������� ������������ ����� c2 ��� c1 ������ �� �������� ��������� void add(c2) �� ������� �����:

 c1.add(c2);

������� ��, ���� ����� ������������ (��� ������� ����� ����� ������) �� ���� ��������� �� ������� (����� ���� � ��������������� ������) ��� ����� ��� ��� �� ������ � ��� ������ ��������� � ��� �� �������. ������ ������� �� � ��������� � � ����� �� ������������ � ������������ �� ���������� �� ���������� �� �����, ����� ���� ���������� �� C++ �� ������� ����:

 c1 += c2;

�������� �� ����, �� ������ airtime �� ���������� timeret.cpp �� ������ 14 ���� �������, ���� �� �� ���� �� ������ ��� ������� ��� ������� ���

 t3 = t1.add(t2);      // add t1 and t2, result in t3

����� � ����� ��-���� � ���������� �� �������������� ������� � ������ 12, ������ ����������

t3.add(t1, t2);       // add t1 and t2, result in t3

� ������� ���� ���-���� � ���������� � ������ �� ����������� ����� � ������������ � �� ������������� ��������� +, ���� �� �� ����� �� �����:

 t3 = t1 + t2;      // add t1 and t2, result in t3

2. ��� �� �������������� �� ���������. ��� ������ �� �� �������, �� �������� �� �� ����� ���� ����� �� �� �������! ������ �������� ���� ������ �� �������� ������ ������, ���� �������� ���� �� ����� employee �� ������ 16. ����� �� ���������� �������� ���������?

 emp3 = emp1 + emp2;

����! (�����, �� �������� �������� ���� �� ����� ��������� � �� �� ������ �� ����� :-))

���� ����, �� ���� �� �� ��������� ���������, ����� �� ���� � ���������� ���� C++ � �� ������ �� ��� ������������ �������. ���� �� ������������ ���� ��������������� ��������� (built-in operators)!. ��������, �� ���� �� �������� ����������� �������� (�������� � ����� operator **) �� ������ ���� �� ������� �� ������ ��� ������� ���� �������� �� ����� ���, ���� �������� operator <-.

����� ����, �� ������ ������������� ��������� ����� �� �� �����������! (You Can�t Overload Everything!). ��������, �� ���� �� ����������� �������� �������� (dot operator .), ��������� �� ������� �� ������� (the scope resolution operator ::), �������� �������� (the conditional operator ?:,), ����� � ����� �����, ����� ��� �� ��� �������.

3. ���������-�������� (The operatorX() Function). � ������ �� ������� �������� X, ����� � ���������� �� ����� Som�Class, ����������� �� ��������� operator X () � ��������:

   SomeClass operator X (SomeClass arg_obj)
   {
      // needs function body
   }

���� � ����� airtime �� ������ 12 �� ���������� ��������� +, ���� �� �� ���� �� �������� ��� ���� (������) ��� ������� ���

   // you can append two strings
   at3 = at2 + at1;

��������� �� ������������ �� ��������� + � ���� ������ �:

   airtime operator + (airtime right)
    {
      // make a temporary object
      airtime temp;
      // add data
      temp.hours = hours + right.hours;
      temp.minutes = minutes + right.minutes;

      // check for carry
      if (temp.minutes >= 60)
      {
        temp.hours++;
        temp.minutes -= 60;
      }

      return temp;          // return temporary object
    }

����� ����� ����, ������ ��������� ���� �� ����� ���� ������� ��������, ��������, ��� � �� ���������, ������ operator + �� ������� operator -, � ���� �� ������� � ������ �� ���������, ���� �� �� �������� ����������� �� ���������.

� �������������� ������� operator + ��� ������� ���������� �������:

(1) ��������� � � ��� operator, ���������� �� ����������, ����� �� ������������ (� ������ +).

(2) ��������� ��� ���� ���� �������� � ��� � �� ���� �� ����� airtime. �� ������ ����� ����� ������� ������� �� ���������� ��������, �.�. ������� t2, � ������ �� �����������

 t3 = t1 + t2;

(3) ������ ������� �� ����������, �� � ������� �� ����� �� ������� ���������: ����� ������, � ������ �����

 t1 +

�� ���������� ����

 t1.operator+()

(4) ��������� ����� �������� �� ���� �� �����, �.�. airtime. ������� �������� � ������ �� ������� ��������� �� ��������� �� t3.

����������, �� ������ ��������� operatorX() ��� ���� ��������� (� ������ 1) � ������� ��-����� �� ���� �������� (� ������ ��� - ����� ���������)!

4. �������� �� �������� �� ������ �� ����� airtime. �������� �������� addair.cpp �� ����� �� ������ [1] ����������� ���������� �� ������ �� ���� ����:

// addair.cpp
// class models time data type
// overloads the + operator

#include <iostream.h>
#include <conio.h>                     // for getch()
#include <iomanip.h>                   // for setw() & fill()

class airtime
{
   private:
      int hours;               // 0 to 23
      int minutes;             // 0 to 59

   public:

      // output to screen
      void display() const
      {
         cout << hours << ':' << setw(2) << setfill('0') << minutes;
      }

      // input from user
      void get()
      {
         char dummy;
         cout << "\nEnter time (format 12:59): ";
         cin >> hours >> dummy >> minutes;
      }

      // overloaded + operator
      airtime operator + (const right) const
      {
         // make a temporary object
         airtime temp;
         // add data
         temp.hours = hours + right.hours;
         temp.minutes = minutes + right.minutes;

         // check for carry
         if (temp.minutes >= 60)
         {
            temp.hours++;
            temp.minutes -= 60;
         }

         return temp;          // return temporary object
      }
};  // end class airtime

void main()
{
   airtime at1, at2, at3;

   cout << "Enter first airtime: ";
   at1.get();

   cout << "Enter second airtime: ";
   at2.get();

   at3 = at1 + at2;         // overloaded + operator
                            //    adds at2 to at1
   cout << "sum = ";
   at3.display();           // display sum

   getch();
}   // main()

����������� �� ������ �� ��������� operator + �� �������, ��� ���� ���������� � ��� �� ������ ���� ���� ��� ��������� airtime add(airtime at2) �� ���������� timeret.cpp �� ������ 14. �������� ��������, �� ���������� �� ����������� �� ���� ������� ��� ����

 // add t1 and t2, result in t3
 t3 = t1.add(t2);

����� �� �������

 // a strange use of operator overloading
 // to add t1 and t2 and get the result in t3
 t3 = t1.operator+(t2);

�� ���� �� �� �����! � ������ �� ���� ����������� ��� ����� ����.

5. �������� �� �������� �� ������ �� ����� xString. �������� �������� strplus �� ����� �� ������ [1] ����������� ���������� �� ������ �� ���� ����:

// strplus.cpp
// overloads + operator to concatenate xStrings

#include <iostream.h>
#include <string.h>                  // for strlen(), strcpy(), etc.
#include <conio.h>                   // for getch()

class xString
{
   private:
      enum { MAX = 80 };             // maximum length of xStrings
      char str[MAX];                 // ordinary C string

   public:
      xString()                      // no-arg constructor
      { strcpy(str, ""); }

      xString( char s[] )            // 1-arg constructor
      { strcpy(str, s); }

      void input()                   // get string from user
      { cin.get(str, MAX); }

      void display()                 // display string
      { cout << str; }
                                     // concatenate two strings

      xString operator+(xString right)
      {
         xString temp;               // temporary xString
         if (strlen(str) + strlen(right.str) < MAX - 1)
         {
            strcpy(temp.str, str);   // copy us to temp
            strcat(temp.str, right.str);  // concatenate argument
         }
         else
            cout << "\nError: xString too long" << endl;
         return temp;                // return temporary
      }
};

void main()
{
   xString s1("Greetings, ");
   xString s2, s3;

   cout << "Enter your name: ";
   s2.input();                   // get s2 from user

   s3 = s1 + s2 + ".";          // concatenate period to s2, s2 to s1
   s3.display();                // display s3

   getch();
}   // main()

� � ���� ��������, ����� � ����������, ��� ��������� operator+ �� ������� ������� ����� temp (� ������ ��� � �� ����� xString), ����� �� ������ ��������� (� ������ - ������������� �� �����������). ��������� ����� �� �������� ���� �����.

6. �������� �� ������ � ���������� �����. ���� � ������ �� ���������� complex3.cpp �� ������ 14, � ����� �������� ������� �� ����� complex_number, add, sub, multdiv �� ��������� � ������������� ���������.

! �� ����������, �� � C++ ��� ��������� ����, ����� �������� ������������ ����� � ���������� � ���. ���� ��, ���������� ���� ��������, ���� � ����������, ����� ������� � ���������� �����, �� ���� �� ������ ����.

// complex4.cpp
// a class that models a complex number data type

#include <iostream.h>
#include <iomanip.h>

class complex_number
{
  private:
    float real;          // real part
    float imaginary;     // imaginery part
  public:
    void set()
    {
      char dummy;        // for comma

      cout << "Enter number (format Re, Im): ";
      cin >> real >> dummy >> imaginary;
    }

    void display()
    {
      cout << '('
           << showpoint
           << setprecision(6) << real       // real part
           << ','
           << setprecision(6) << imaginary  // imaginary part
           << ')';
    }

    // add to the complex number another one (compl_num)
    complex_number operator + (complex_number compl_num)
    {
      complex_number cn;
      cn.real = real + compl_num.real;
      cn.imaginary = imaginary + compl_num.imaginary;

      return cn;
    }

    // subtract from the complex number another one (compl_num)
    complex_number operator - (complex_number compl_num)
    {
      complex_number cn;
      cn.real = real - compl_num.real;
      cn.imaginary = imaginary - compl_num.imaginary;

      return cn;
    }

    // multiply two complex numbers and assign the result to third one
    complex_number operator * (complex_number compl_num)
    {
      complex_number cn;

      cn.real = real*compl_num.real - imaginary*compl_num.imaginary;
      cn.imaginary = real*compl_num.imaginary + compl_num.real*imaginary;

      return (cn);
    }

    // divide the complex number by another one (compl_num)
    complex_number operator / (complex_number compl_num)
    {
      complex_number cn;

      cn.real = real;
      cn.imaginary = imaginary;

      float sqr_mod = compl_num.module();
      if (sqr_mod == 0)
      {
        cout << "Devision by zero!" << endl;
        return cn;
      }

      sqr_mod *= sqr_mod;

      cn.real = (real*compl_num.real + compl_num.imaginary*imaginary)/sqr_mod;
      cn.imaginary = (compl_num.real*imaginary - real*compl_num.imaginary)/sqr_mod;

      return cn;
    }

    // calculate the module of the complex number
    float module()
    {
      float m = real*real + imaginary*imaginary;
      return sqrt(m);
    }

};  // class complex_number

void main()
{
  // create three complex_number variables
  complex_number c1, c2, c3;
  char choice;

  do
  {
    // enter c1
    cout << "For c1, ";
    c1.set();       // set c1

    // enter c1
    cout << "For c2, ";
    c2.set();       // set c2
    cout << endl;

    // perform addition and ...
    c3 = c1 + c2;
    // ... display the result
    c1.display();
    cout << " + ";
    c2.display();
    cout << " = ";
    c3.display();
    cout << endl << endl;

    // perform subtraction and ...
    c3 = c1 - c2;
    // ... display the result
    c1.display();
    cout << " - ";
    c2.display();
    cout << " = ";
    c3.display();
    cout << endl << endl;

    // perform multiplication and ...
    c3 = c1 * c2;
    // ... display the result
    c1.display();
    cout << " * ";
    c2.display();
    cout << " = ";
    c3.display();
    cout << endl << endl;

    // perform division and ...
    c3 = c1 / c2;
    // ... display the result
    c1.display();
    cout << " / ";
    c2.display();
    cout << " = ";
    c3.display();
    cout << endl << endl;

    cout << "Do another (y/n)? ";
    cin >> choice;
  } while(choice != 'n');

}  //end main

��� �������� ���� �� ����� �������� complex3.cppcomplex4.cpp �� ���������� ����� �������, � ���������� �� ����, �� �������� ������� � complex3.cpp �� ���������� � ���������� ����� �� ������� � ����������� ��������� � ������� �� ����������� �� ����������� � �������� ������� �:

  c3 = c1 + c2;

������

  c3 = c1.add(c2);

  c3 = c1 - c2;

������

  c3 = c1.sub(c2);

  c3 = c1 * c2;

������

  c3 = c1.mult(c2);

  c3 = c1 / c2;

������

  c3 = c1.div(c2);

�������� ��������, �� ��������� float module() �� ���� �� ���� ������� � ���������� ��������, ������� �����, ������ ����� ���� � ���������� ���� C++. �� � ���������� ����� �� �� ����� �� ���� ������������� �� ���������� ����� � ������, ���� ��� ���� �� �������� ����������� �� ������ �� ������������ �����.

������� �� ������������ �� ���������� ��� ���������� � ������������ ����� 2i5i � ��������:

For c1, Enter number (format Re, Im): 0,2
For c2, Enter number (format Re, Im): 0,5

(0.00000,2.00000) + (0.00000,5.00000) = (0.00000,7.00000)

(0.00000,2.00000) - (0.00000,5.00000) = (0.00000,-3.00000)

(0.00000,2.00000) * (0.00000,5.00000) = (-10.0000,0.00000)

(0.00000,2.00000) / (0.00000,5.00000) = (0.400000,0.00000)

Do another (y/n)?

���������� ������ �� �� ��������� � �������������� �� ������� ������� ���������.
.

(����������)
.
����������
.
[1] Robert Lafore; C++ Interactive Course. Waite Group Press, Macmillan Computer Publishing, 1996.
.
�����: ����. �������� �����������
.
[ ���� � �������� �� ���� 44 �� �������� 2010 � �� �������� "������" www.kosnos.com ]
.
������� ����: ���� , �����, ������� ����������� ������������ , ������������
Keywords: �++,  OOP programming , C++ , Classes , Inheritance , Reusability , Creating New Data Types
OPERATOR OVERLOADING
Overloading Binary Arithmetic Operators The operatorX() Function Arguments Return Value
Overloading Other Binary Operators Overloading Relational Operators Passing the Argument by const Reference Assignment Operators Avoiding Temporary Objects
Overloading Unary Operators Prefix Version of Operator ++ Postfix Version of Operator ++ The Unary Minus Operator
Conversion from Objects to Basic Types  Type Casting: Conversion for Basic Types Conversion Function Invoked Automatically Casting for Clarity A Static Constant The static_cast Approach
Conversions Between Classes The One-Argument Constructor
Overloading the Assignment Operator (=) Syntax of the Overloaded Assignment Operator An Assignment Operator That Allows Chaining
Overloading the [ ] Operator Access with access() Function Access with Overloaded [ ] Operator
Fine-Tuning Overloaded Operators Constant Arguments Constant Functions Constant Overloaded Operators
Returns from Assignment Operators The Amazing *this Object The += Operator Revisited The Increment Operator Revisited