binary operators

operators that function on two operands are called binary operators.

syntax

:

1
return_type operator_type (parameter1, parameter2);

when it implement as a class member the syntax only has one parameter:

1
return_type operator_type (parameter);

the second parameter us usually derived from the attributes of class itself.

types of binary operators

table of overloadable binary operators

operator name
, Comma
!= Inequality
% Modulus
%= Modulus/assignment
& Bitwise AND
&& Logical AND
&= Bitwise AND/assignment
* Multiplication
*= Multiplication/assignment
+ Addition
+= Addition/assignment
- Subtraction
-= Subtraction/assignment
->* Pointer-to-member selection
/ Division
/= Division/assignment
< Less than
<< Left shift
<<= Left shift/assignment
<= Less than or equal to
= Assignment, Copy Assignment and Move Assignment
== Equality
> Greater than
>= Greater than or equal to
>> Right shift
>>= Right shift/assignment
^ Exclusive OR
^= Exclusive OR/assignment
| Bitwise inclusive OR
|= Bitwise inclusive OR/assignment
|| Logical OR
[] Subscript operator

programming binary addition + and subtraction- operators

Similar to the increment/decrement operators, the binary plus and minus, when defined, enable you to add or subtract the value of a supported date type from an object of the class that implements these operators.

e.g. calendar class featuring the binary addition operatordemolist12_4

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
#include <iostream>
using namespace std;

class Date
{
private:
int day_, month_, year_;

public:
Date(int input_day, int input_month, int input_year) : day_(input_day), month_(input_month), year_(input_year){};
Date operator+(int days_add)
{
Date newDate(day_ + days_add, month_, year_);
return newDate;
}
Date operator-(int days_add)
{
Date newDate(day_ - days_add, month_, year_);
return newDate;
}
void DisplayDate()
{
cout << day_ << "/" << month_ << "/" << year_ << endl;
}
};

int main()
{
Date TodayDate(6, 8, 2023);
Date NextFiveDaysDate(TodayDate + 5);
cout << "next five days date is ";
NextFiveDaysDate.DisplayDate();
Date PreviousFiveDaysDate(TodayDate - 5);
cout << "previous five days date is ";
PreviousFiveDaysDate.DisplayDate();

return 0;
}

Line 11 to 20 contain the implementation of the binary operator + and -, that permit the use of simple addition and subtraction syntax as seen in main in line 30 and 33 respectively.

implementing addition assignment += and subtract assignment -=

the syntax a+=b is that incrementing the value of an object a by an amount b.

e.g. defining += and -= to add and subtract days in the calendar given integer input demolist12_5

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
#include <iostream>
using namespace std;

class Date
{
private:
int day_, month_, year_;

public:
Date(int input_day, int input_month, int input_year) : day_(input_day), month_(input_month), year_(input_year){};
void operator+=(int days_add)
{
day_ += days_add;
}
void operator-=(int days_add)
{
day_ -= days_add;
}

void DisplayDate()
{
cout << day_ << "/" << month_ << "/" << year_ << endl;
}
};

int main()
{
Date TodayDate(6, 8, 2023);
TodayDate += 5;
cout << "next five days date is ";
TodayDate.DisplayDate();
TodayDate -= 5;
cout << "previous five days date is ";
TodayDate.DisplayDate();

return 0;
}

The addition and subtraction assignment operators are in line 11 to 18. Additionally, addition(subtract) assignment operator can be used in overloaded function:

1
2
3
4
void operator+= (const Day& DaysToAdd)
{
day_+=DaysToAdd.GetDays();
}

the difference between void operator + and Date operator +.

void operator +:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
 void operator+(int days_add)
{
day_ = day_ + days_add;
}
Date operator-(int days_add)
{
Date newDate(day_ - days_add, month_, year_);
return newDate;
}
...
int main()
{
Date TodayDate(6, 8, 2023);
TodayDate + 5;
cout<< "next five days date is ";
TodayDate.DisplayDate();
Date PreviousFiveDaysDate(TodayDate - 5);
cout << "previous five days date is ";
PreviousFiveDaysDate.DisplayDate();
}

output_1

1
2
next five days date is 11/8/2023
previous five days date is 6/8/2023

whereas, Date operator +:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
Date operator+(int days_add)
{
Date newDate(day_ + days_add, month_, year_);
return newDate;
}

Date operator-(int days_add)
{
Date newDate(day_ - days_add, month_, year_);
return newDate;
}
...
int main()
{
Date TodayDate(6, 8, 2023);
Date NextFiveDaysDate(TodayDate + 5);
cout << "next five days date is ";
NextFiveDaysDate.DisplayDate();
Date PreviousFiveDaysDate(TodayDate - 5);
cout << "previous five days date is ";
PreviousFiveDaysDate.DisplayDate();
}

output_2

1
2
next five days date is 11/8/2023
previous five days date is 1/8/2023

the difference lies in the value of day_, it doesn't change in Date operator + version but is undoubtedly changed respectively as shown in output_1 and output_2.

== and != operator

overloading equality (==) and overloading inequality (=-) are used in comparison. Syntax like:

1
2
3
4
5
6
7
8
if (date1 == date2)
{
//do something
}
else
{
//do something else
}

The binary comparison will work for instance of classes containing simple data types, but it will nor work if the class in question has a non-static string member(char*).

Comparisons involving two instances of MyString in demolist9_9 would return false consistently. solving this problem by defining comparison operators. A generic expression of equality operator is the following:

1
2
3
4
bool operator ==(const ClassType& CompareTo)\
{
//comparison...
}

similar syntax for inequality operator(!=).

e.g. demonstrates operators == and != demolist12_6

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
#include <iostream>
using namespace std;

class Date
{
private:
int day_, month_, year_;

public:
Date(int input_day, int input_month, int input_year) : day_(input_day), month_(input_month), year_(input_year){};

bool operator==(const Date &CompareTo)
{
return ((day_ == CompareTo.day_) &&
(month_ == CompareTo.month_) &&
(year_ == CompareTo.year_));
}
bool operator!=(const Date &CompareTo)
{
return !(this->operator==(CompareTo));
}

void DisplayDate()
{
cout << day_ << "/" << month_ << "/" << year_ << endl;
}
};
int main()
{
Date YourDate1(9, 8, 2023);
Date YourDate2(3, 8, 2023);

if (YourDate1 == YourDate2)
cout << "same day" << endl;
else
cout << "different day" << endl;

if (YourDate1 != YourDate2)
cout << "different day" << endl;
else
cout << "same day" << endl;

return 0;
}

Note that this pointer used in line 20, it points to object of class Date. Hence, it call operator == in return !(this->operator==(CompareTo));