## Overloading of shortened assignment operators +=, -=, *=, /=, %=. Examples

This topic is based on the following topics.

### Contents

##### 1. Features of overloading of abbreviated assignment operators +=, -=, *=, /=, %=. The general form of the operator function

The shortened assignment operators +=, -=, *=, /=,%= are binary, that is, they require two operands for calculations.
If an operator function is implemented in a class, then it receives one operand and has the following general form

```class ClassName
{
// ...

// operator function that overloads one of the shortened assignment operators
ClassName operator##(ClassName obj)
{
// ...
}
}```

here

• ClassName – the name of the class for which the operator function operator##() is implemented;
• operator##() – some operator function. The ## symbols are replaced with the symbols +=, -=, *=, /=, %=, that is, the operator function will have the following names, respectively: operator+=(), operator-=(), operator*=(), operator/=(), operator%=().
• obj – instance (object) of class ClassName.

If the operator function is implemented as “friendly”, then it receives two operands. In this case, the general form of using an operator function is:

```class ClassName
{

// ...

// "friendly" operator function,
// which overloads one of the shortened assignment operators
friend ClassName operator##(ClassName& obj1, ClassName& obj2)
{
// ...
}
}

// implementation of the "friendly" to the class ClassName operator function
ClassName operator##(ClassName& obj1, ClassName& obj2)
{
// ...
}```

here obj1, obj2 – a reference to instances of the ClassName class, which are passed to the operator-friendly operator##() function.

### ⇑

##### 2. An example that demonstrates overloading the += operator for the class Integer, which implements an integer. The operator function is declared inside the class

An Integer class is specified that implements operations on an integer value. The class declares:

• internal variable of integer type, which stores the value;
• two constructors;
• methods for accessing the integer variable number;
• operator function operator+=(), which overloads the shortened assignment operator +=.

The implementation of class Integer

```// class Integer - implements an integer
class Integer
{
private:
int number;

public:
// class constructors
Integer() { number = 0; }
Integer(int _number) { number = _number; }

// access methods
int Get(void) { return number; }
void Set(int _number) { number = _number; }

Integer operator+=(Integer obj)
{
number = number + obj.number; // access by reference
return *this;
}
};```

Using an instance (object) of the Integer class in another program code can be, for example, the following:

```// overloading of shortened assignment operators +=, -=, etc.
Integer d1(10), d2(18); // instances of the Integer class
int t;

// invoke the operator function operator+=()
d1+=d2; // d1.number = d1.number + d2.number
t = d1.Get(); // t = 28```

According to this pattern, you can overload the other shortened assignment operators -=, *=, /=, %=.

### ⇑

##### 3. An example that demonstrates overloading the %= operator for the Integer class, which implements an integer. The operator function is declared as a “friendly” to the class Integer

In the Integer class, the friend function operator%=() is declared. This function returns the remainder of the division of objects. Objects are the input parameters of the function.

```// the Integer class that implements an integer
class Integer
{
private:
int number;

public:
// class constructors
Integer() { number = 0; }
Integer(int _number) { number = _number; }

// access methods
int Get(void) { return number; }
void Set(int _number) { number = _number; }

// declaration the "friendly" to the class Integer operator function
friend Integer operator%=(Integer& obj1, Integer& obj2);
};

// "friendly" operator function to the class Integer
// the reference to the class Integer is passed to the operator function.
Integer operator%=(Integer& obj1, Integer& obj2)
{
obj1.number = obj1.number % obj2.number; // access by reference
return obj1;
}```

The use of the operator function operator%=() can be as follows

```// overloading of shortened assignment operators +=, -=, etc.
Integer d1(14), d2(6); // instances of the Integer class
int t;

// invoking the "friendly" operator function operator%=()
d1 %= d2; // d1.number = d1.number + d2.number
t = d1.Get(); // t = 2

Integer d3(9), d4(5);
d3 %= d4;
t = d3.Get(); // t = 4```

### ⇑

##### 4. An example that demonstrates overloading the *= operator for the Complex class that implements a complex number. The operator function is implemented inside the class.

The Complex class implements an operator function that overloads the *= operator. This function implements the multiplication of complex numbers according to the formula:

`(a+bj)*(c+dj) = (a*c-b*d) + (a*d+b*c)j`

Implementation of class Complex

```class Complex
{
private:
double real; // real part
double imag; // imaginary part

public:
// class constructor
Complex(double _real, double _imag)
{
real = _real;
imag = _imag;
}

// access methods
double GetReal(void) { return real; }
double GetImag(void) { return imag; }

void Set(double _real, double _imag)
{
real = _real;
imag = _imag;
}

// operator function that overloads the operator *=
// the function implements multiplication of complex numbers
Complex operator*=(Complex obj)
{
double r;
double i;
r = real*obj.real - imag*obj.imag;
i = real*obj.imag + obj.real*imag;
real = r;
imag = i;
return *this;
}
};```

Demonstration of using the operator function

```// Overloading the shortened operator *=
Complex c1(2,3); // 2+3j
Complex c2(-1,1); // -1+j
double t;

// invoking the operator function *=
c1 *= c2; // c1 = -5-1j

t = c1.GetReal(); // t = -5
t = c1.GetImag(); // t = -1```

### ⇑

##### 5. An example that demonstrates operator overloading *= for the Complex class that implements a complex number. The operator function is implemented as a “friendly” to the class

The example implements the operation of multiplying complex numbers using the “friend” operator function operator*=(). The “friendly” operator function receives 2 parameters.

```class Complex
{
private:
double real; // real part
double imag; // imaginary part

public:
// class constructor
Complex(double _real, double _imag)
{
real = _real;
imag = _imag;
}

// access methods
double GetReal(void) { return real; }
double GetImag(void) { return imag; }

void Set(double _real, double _imag)
{
real = _real;
imag = _imag;
}

// declaration of "friendly" function
friend Complex operator*=(Complex& c1, Complex& c2);
};

// implementation of the "class-friendly" function to the Complex class
Complex operator*=(Complex& c1, Complex& c2)
{
double r;
double i;
r = c1.real * c2.real - c1.imag * c2.imag;
i = c1.real * c2.imag + c1.imag * c2.real;
c1.real = r;
c1.imag = i;
return c1;
}```

Using the Complex class in another method can be as follows:

```// Overload the shortened operator *=
Complex c1(3,-2); // 3-2j
Complex c2(1,-1); // 1-j

double t;

// invoke the operator function *=
c1 *= c2;

t = c1.GetReal(); // t = -1
t = c1.GetImag(); // t = -5```

### ⇑

##### 6. An example that demonstrates overloading the /= operator for an array of doubles. The operator function is implemented inside the class.

The ArrayDouble class implements an array of doubles. Operator function operator/=() implements the element-wise division of two arrays

```// a fixed array of type double
class ArrayDouble
{
private:
double D; // array

public:
// constructor
ArrayDouble()
{
for (int i=0; i<10; i++)
D[i] = 0;
}

// access methods
double Get(int index)
{
if ((index>=0)&&(index<10))
return D[index];
return 0.0;
}

void Set(int index, double value)
{
if ((index>=0)&&(index<10))
D[index] = value;
}

// operator function, elementwise divides two arrays
{
// elementwise division of the current array into an array of AD
for (int i=0; i<10; i++)
return *this;
}
};```

Using the ArrayDoble class can be as follows

```// Overloading of shortened operator *=
ArrayDouble A1;
ArrayDouble A2;
double t;

// arrays forming
for (int i=0; i<10; i++)
A1.Set(i,10); // A1 = {10,10,10,10,10,10,10,10,10,10}

for (int i=0; i<10; i++) // A2 = {1,2,3,4,5,6,7,8,9,10}
A2.Set(i, i+1);

// invoke the operator function
A1 /= A2;

// checking
t = A1.Get(0); // t = 10/1 = 10.0
t = A1.Get(1); // t = 10/2 = 5.0
t = A1.Get(2); // t = 10/3 = 3.33333333
t = A1.Get(7); // t = 10/8 = 1.25```

### ⇑

##### 7. An example that demonstrates overloading the /= operator for an array of doubles. Operator function implemented as “class-friendly”

In the ArrayDouble class, a “friendly” operator function is declared that implements elementwise division of arrays. Declaring class ArrayDouble and operator function is as follows

```// fixed array of type double
class ArrayDouble
{
private:
double D; // array

public:
// constructor
ArrayDouble()
{
for (int i=0; i<10; i++)
D[i] = 0;
}

// access methods
double Get(int index)
{
if ((index>=0)&&(index<10))
return D[index];
return 0.0;
}

void Set(int index, double value)
{
if ((index>=0)&&(index<10))
D[index] = value;
}

// declaration of "friendly" operator functions
};

// implementation of the class-friendly ArrayDouble operator function
{
for (int i=0; i<10; i++)
}```

Using a class in another program code

```// Overloading of shortened operator *=
ArrayDouble A1;
ArrayDouble A2;
double t;

// arrays forming
for (int i=0; i<10; i++)
A1.Set(i,10); // A1 = {10,10,10,10,10,10,10,10,10,10}

for (int i=0; i<10; i++) // A2 = {1,2,3,4,5,6,7,8,9,10}
A2.Set(i, i+1);

// invoke the "friendly" operator functions
A1 /= A2;

// checking
t = A1.Get(0); // t = 10/1 = 10.0
t = A1.Get(1); // t = 10/2 = 5.0
t = A1.Get(2); // t = 10/3 = 3.33333333
t = A1.Get(7); // t = 10/8 = 1.25```

### ⇑

##### 8. An example that demonstrates several options for overloading the += operator for the Point class. The operator function operator+=() is overloaded and implemented inside the class

Operator function in the class can be overloaded. Such functions must contain the same number of parameters, but these parameters must be of different types.

```// A class that implements a point on the coordinate plane
// class contains two overloaded operator functions operator+=()
class Point
{
private:
int x, y; // coordinates of a point

public:
// class constructors
Point()
{
x = y = 0;
}

Point(int nx, int ny)
{
x = nx;
y = ny;
}

int GetX(void) { return x; }
int GetY(void) { return y; }
void SetX(int nx) { x = nx; }
void Set(int ny) { y = ny; }

// operator function '+='
// which sums the two points
Point operator+=(Point pt)
{
x = x + pt.x;
y = y + pt.y;
return *this;
}

// operator functions '+='
// which increases x and y by the value of the input parameter d
Point operator+=(int d)
{
x = x + d;
y = y + d;
return *this;
}
};```

In the above code, there are two operator functions operator+=() that overload the operator +=.

```Point operator+=(Point pt)
{
// ...
}

Point operator+=(int d)
{
// ...
}```

The following demonstrates the use of the class Point operator functions.

```// overloading the shortened operatror +=
Point p1(2,3);
Point p2(4,5);
int x, y;

// invoke the operator function of summation of instances of type Point
p1 += p2;

// checking
x = p1.GetX(); // x = 2+4 = 6
y = p1.GetY(); // y = 3+5 = 8

p2 += p1;
x = p2.GetX(); // x = 6+4 = 10
y = p2.GetY(); // y = 8+5 = 13

// invoke the operator function of summation Point + int
p1 += 10;
x = p1.GetX(); // x = 6+10 = 16
y = p1.GetY(); // y = 8+10 = 18

//
Point p3(5,5);
p3+=-7;
x = p3.GetX(); // x = -2
y = p3.GetY(); // y = -2```