binary operators
operators that function on two operands are called binary operators
.
:
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 |
|
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 |
|
The addition and subtraction assignment operators are in line 11 to 18. Additionally, addition(subtract) assignment operator can be used in overloaded function:
1 | void operator+= (const Day& DaysToAdd) |
the difference between void operator +
and Date operator +
.
void operator +
:
1 | void operator+(int days_add) |
output_1
1 | next five days date is 11/8/2023 |
whereas, Date operator +
:
1 | Date operator+(int days_add) |
output_2
1 | next five days date is 11/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 | if (date1 == date2) |
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 | bool operator ==(const ClassType& CompareTo)\ |
similar syntax for inequality operator
(!=).
e.g. demonstrates operators == and != demolist12_6
1 |
|
Note that this
pointer used in line 20, it points to object of class Date
. Hence, it call operator == in return !(this->operator==(CompareTo));