Operator
◦ An operator is a symbol that tells the compiler to perform specific mathematical or
logical manipulations. C++ is rich in built-in operators and provide the following types
of operators −
◦ Arithmetic Operators
◦ Relational Operators
◦ Logical Operators
◦ Bitwise Operators
◦ Miscellaneous Operators
◦ Increment / Decrement Operators
◦ Scope resolution Operators
Arithmetic Operators
◦ There are following arithmetic operators supported by C++ language −
◦ Assume variable A holds 10 and variable B holds 20.
Operator Description Example
+ Adds two operands A + B will give 30
- Subtracts second operand from the first A - B will give -10
* Multiplies both operands A * B will give 200
/ Divides numerator by de-numerator B / A will give 2
% Modulus Operator and remainder of after an B % A will give 0
integer division
Relational Operators
Operat Description Example
or
== Checks if the values of two operands are (A == B) is not
equal or not, if yes then condition becomes true.
true.
!= Checks if the values of two operands are (A != B) is true.
equal or not, if values are not equal then
condition becomes true.
> Checks if the value of left operand is greater (A > B) is not
than the value of right operand, if yes then true.
condition becomes true.
< Checks if the value of left operand is less than (A < B) is true.
the value of right operand, if yes then condition
becomes true.
>= Checks if the value of left operand is greater (A >= B) is not true.
than or equal to the value of right operand, if yes
then condition becomes true.
<= Checks if the value of left operand is less than or (A <= B) is true.
equal to the value of right operand, if yes then
condition becomes true.
Logical Operators
Operat Description
or
&& Returns true if both statements are true
|| Returns true if one of the statements is true
! Reverse the result, returns false if the result is true
Bitwise Operators
Operator Description
& Binary AND Operator copies a bit to the result if it exists in both
operands.
| Binary OR Operator copies a bit if it exists in either operand.
^ Binary XOR Operator copies the bit if it is set in one operand but not
both.
Miscellaneous
◦ sizeof – It returns the memory occupied by the particular data type
of the operand
◦ & (Pointer) – It refers to the address (memory location) in which
the operand is stored.
◦ * (Pointer) – It is a pointer operator
◦ ? (Condition) – It is an alternative for if-else condition
Increment Operators
◦ The increment operator is used to increment the value of a variable in an expression.
◦ In the Pre-Increment, value is first incremented and then used inside the expression.
◦ Whereas in the Post-Increment, value is first used inside the expression and then incremented.
◦ Syntax:
◦ // PREFIX
◦ ++m
◦ // POSTFIX
◦ m++
◦ where m is a variable
Decrement Operators
◦ The decrement operator is used to decrement the value of a variable in an
expression.
◦ In the Pre-Decrement, value is first decremented and then used inside the
expression.
◦ Whereas in the Post-Decrement, value is first used inside the expression and then
decremented.
◦ Syntax:
◦ // PREFIX
◦ --m
◦ // POSTFIX
◦ m--
◦ where m is a variable
Scope resolution operator
◦ The scope resolution operator :: is used to identify and disambiguate identifiers
used in different scopes.
◦ Access a global data variable when both the global and the local variable share the
same name/label.
◦ Enable us to define a member function outside a class.
◦ Referring to a class inside another class.
◦ To access the static variables of a class.