# Overloading operations in C++. Oop

In C++, it is possible to overload not only functions, but also operations within a class, for example, it is possible to ensure that the operation * when working with matrices multiplies matrices, and when working with complex numbers - multiplication of complex numbers.

To overload operations within a class, you need to write a special function, a method of the class. When overloading operations, keep the following in mind:

- You cannot change the priority of operations.
- you cannot change the type of operation (you cannot make a binary operation out of an unary operation or vice versa);
- An overloaded operation is a member of a class and can only be used in expressions with objects of its class.
- You cannot create new operations.
- it is forbidden to overload operations:
**.**(access to class members), unary operation*****(value at pointer address),**::**(scope extension),**?:**(**if**operation); - the following operations can be overloaded:
**+**,**—**,*****,**/**,**%**,**=**,**<**,**>**,**+=**,**-=**,***=**,**/=**,**&&**,**||**,**++**,**—**,**()**,**[]**,**new**,**delete**.

To overload a binary operation inside a class, you must create a method function:

1 2 3 4 | type operator symbols(type1 parametr) { operators; } |

Here**, type** is the type of value returned by the operation, **operator** is the service word, **symbols** is an overloaded operation, **type1** is the type of the second operand, the first operand is an instance of the current class, **parametr** is the variable name of the second operand.

As an example, consider the complex class with overloaded operations + (addition of complex numbers) and — (subtraction of complex numbers).

#### Task

Create a class for working with complex numbers in which to overload addition and subtraction operations. The text of the program with comments is shown below.

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 | #include "stdafx.h" #include <iostream> using namespace std; complex class class complex { public: constructor of the complex class (bool pr=true); A method that overloads the complex operator +(complex M) addition operation. A method that implements an overload of the subtraction operation complex operator —(complex M); the actual part of the complex number float x; the imaginary part of the complex number float y; the method of displaying a complex number on the void screen show_complex(); }; the main function int main() { setlocale(LC_ALL,"Rus");; complex chislo, chislo1, chislo2, chislo3(false), chislo4(false); In the complex class, the + operation is overloaded to add complex numbers to add complex numbers chislo3=chislo1+chislo2; cout<<«chislo3"; chislo3. show_complex(); to subtract two complex numbers, it is enough to use the operation -. In the complex class, the operation is overloaded to perform subtraction of complex numbers chislo4=chislo1—chislo2; cout<<"chislo4"; chislo4. show_complex(); system(«pause»); return 1; } constructor of the complex class with a Boolean parameter (true- default). if the parameter is true, the constructor will query the imaginary and actual parts of the number, but if the constructor parameter is 0, a complex number with zero real and imaginary parts will be created . These numbers can be used to create variables that store the result of actions with complex complex::complex(bool pr) { if (pr) { cout<< "enter x \t"; cin>>x; cout<< "enter y \t"; cin>>y; show_complex(); } else {x=0; y=0; } } method for displaying a complex number on the screen void complex::show_complex() { if (y>=0) cout<<x<<"+" <<y<<"i"<<endl; else cout<<x<<y<<"i"<<endl; } a method that implements an overload of the addition operation. The result of this function will be a new complex complex number ::operator+(complex M) { create a complex temp number in which the result of adding two complex temp(false) numbers will be stored. ); the actual part of the new complex number is formed as a result of adding the real parts of the first and second operand, the first is the current class, the second is the complex number M temp passed to the function. x=x+M.x; the imaginary part of the new complex number is formed as a result of the addition of the imaginary parts of the first and second operand temp. y=y+M.y; return the sum of two complex numbers as a return temp result ; } a method that overloads the subtraction operation complex::operator—(complex M) { complex temp(false); the actual part of the new complex number is formed as a result of subtracting the actual parts of the first and second operand, the first is the current class, the second is the complex number M temp passed to the function. x=x—M.x; the imaginary part of the new complex number is formed as a result of subtracting the imaginary parts of the first and second operand temp. y=y—M.y; return the difference between two complex numbers return temp; }; |

To overload an unary operation inside a class, you must create a method function:

1 2 3 4 | type operator symbols() { operators; } |

As you may recall, in C++ the unary operations ++ and —. The effect of these operations differs when positioned on the left and to the right of the operand. Let's consider how the overload of the operation ++x and x++ is implemented using the example of a class of complex numbers. Let the operation ++x increment the real and imaginary parts of the complex number x by 1, and x++ increment by 1 only the actual part of the complex number x.

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 | #include "stdafx.h" #include <iostream> using namespace std; class complex { public: complex(bool pr=true) { if (pr) { cout<< "enter x \t"; cin>>x; cout<< "enter y \t"; cin>>y; show_complex(); }; a function that overloads the operator ++x, in which case this method without the parameters complex operator++() { increment the actual and imaginary parts by 1 x++; y++; return the current class as the result of the return *this function ; } a function that overloads the x++ operator, in this case it is a method with an abstract parameter of an integer type. the presence of an integer type in parentheses only indicates that the x++ operator is overloaded, not ++x complex operator++(int) { increase the actual and imaginary parts by 1 x++; return *this; } method of displaying a complex number on the screen void show_complex() { if (y>=0) cout<<x<<"+"<<y<<"i" <<endl; else cout<<x<<y<<"i"<<endl; }; float x; float y; }; int main() { setlocale(LC_ALL,«Rus»); complex number chislo2 complex chislo2; increase it by 1 by calling the complex operator++() ++chislo2 method; output of the complex number cout<<"++chislo2 = "; chislo2. show_complex(); increment it by 1 by calling the complex operator++(int) chislo2++ method; output of the complex number cout<<"chislo2++ = "; chislo2. show_complex(); system(«pause»); return 1; } |

As you can see, the ++x and x++ operators were overloaded in different ways. You can add methods to the complex class that overload the multiplication and division operations of complex numbers.