Operators in C++ (original) (raw)

C++ operators are the symbols that operate on values to perform specific mathematical or logical computations on given values. They are the foundation of any programming language.

C++ `

#include using namespace std; int main() {

int a = 10 + 20;

cout << a;
return 0;

}

`

Explanation: Here, '****+**' is an **addition operator and does the addition of 10 and 20 operands and return value 30 as a result.

C++ Operator Types

C++ operators are classified into **6 types on the basis of type of operation they perform:

Arithmetic Operators

Arithmetic operators are used to perform arithmetic or mathematical operations on the operands. For example, '****+**' is used for addition.

#include using namespace std;

int main() { int a = 8, b = 3;

// Addition
cout << "a + b = " << (a + b) << endl;

// Subtraction
cout << "a - b = " << (a - b) << endl;

// Multiplication
cout << "a * b = " << (a * b) << endl;

// Division
cout << "a / b = " << (a / b) << endl;

// Modulo
cout << "a % b = " << (a % b) << endl;

// Increament
cout << "++a = " << ++a << endl;

// Decrement
cout << "b-- = " << b--;

return 0;

}

`

Output

a + b = 11 a - b = 5 a * b = 24 a / b = 2 a % b = 2 ++a = 9 --b = 2

Important Points:

You may have noticed that some operator works on two operands while other work on one. On the basis of this operators are also classified as:

Relational Operators

Relational operators are used for the comparison of the values of two operands. For example, '>' check right operand is greater.

#include using namespace std;

int main() { int a = 6, b = 4;

// Equal operator
cout << "a == b is " << (a == b) << endl;

// Greater than operator
cout << "a > b is " << (a > b) << endl;

// Greater than Equal to operator
cout << "a >= b is " << (a >= b) << endl;

//  Lesser than operator
cout << "a < b is " << (a < b) << endl;

// Lesser than Equal to operator
cout << "a <= b is " << (a <= b) << endl;

// Not equal to operator
cout << "a != b is " << (a != b);

return 0;

}

`

Output

a == b is 0 a > b is 1 a >= b is 1 a < b is 0 a <= b is 0 a != b is 1

Note: 0 denotes false and 1 denotes true.

Logical Operators

Logical operators are used to combine two or more conditions or constraints or to complement the evaluation of the original condition in consideration. The result returns a Boolean value, i.e., true or false.

#include using namespace std;

int main() { int a = 6, b = 4;

// Logical AND operator
cout << "a && b is " << (a && b) << endl;

// Logical OR operator
cout << "a || b is " << (a || b) << endl;

// Logical NOT operator
cout << "!b is " << (!b);

return 0;

}

`

Output

a && b is 1 a || b is 1 !b is 0

**Bitwise Operators

Bitwise operators are works on bit-level. So, compiler first converted to bit-level and then the calculation is performed on the operands.

**Note: Only char and int data types can be used with Bitwise Operators.

C++ `

#include using namespace std;

int main() { int a = 6, b = 4;

// Binary AND operator
cout << "a & b is " << (a & b) << endl;

// Binary OR operator
cout << "a | b is " << (a | b) << endl;

// Binary XOR operator
cout << "a ^ b is " << (a ^ b) << endl;

// Left Shift operator
cout << "a << 1 is " << (a << 1) << endl;

// Right Shift operator
cout << "a >> 1 is " << (a >> 1) << endl;

// One’s Complement operator
cout << "~(a) is " << ~(a);

return 0;

}

`

Output

a & b is 4 a | b is 6 a ^ b is 2 a<<1 is 12 a>>1 is 3 ~(a) is -7

Assignment Operators

Assignment operators are used to assign value to a variable. We assign the value of right operand into left operand according to which assignment operator we use.

#include using namespace std;

int main() { int a = 6, b = 4;

// Assignment Operator.
cout << "a = " << a << endl;

//  Add and Assignment Operator.
cout << "a += b is " << (a += b) << endl;

// Subtract and Assignment Operator.
cout << "a -= b is " << (a -= b) << endl;

//  Multiply and Assignment Operator.
cout << "a *= b is " << (a *= b) << endl;

//  Divide and Assignment Operator.
cout << "a /= b is " << (a /= b);

return 0;

}

`

Output

a = 6 a += b is 10 a -= b is 6 a *= b is 24 a /= b is 6

Ternary or Conditional Operators

Conditional operator returns the value, based on the condition. This operator takes **three operands, therefore it is known as a **Ternary Operator.

**Syntax:

C++ `

Expression1 ? Expression2 : Expression3

`

In the above statement:

#include using namespace std;

int main() { int a = 3, b = 4;

// Conditional Operator
int result = (a < b) ? b : a;
cout << "The greatest number "
      "is " << result;

return 0;

}

`

Output

The greatest number is 4

Miscellaneous Operators

Apart from these operators, there are a few operators that do not fit in any of the above categories. These are:

**sizeof Operator

sizeof operator is a unary operator used to compute the size of its operand or variable in bytes. For example,

C++ `

sizeof (char); sizeof (var_name);

`

**Comma Operator (,)

Comma operator is a binary operator that is used for multiple purposes. It is used as a separator or used to evaluate its first operand and discards the result; it then evaluates the second operand and returns this value (and type).

C++ `

int n = (m+1, m-2, m+5); int a, b, c;

`

**Addressof Operator (&)

Addressof operator is used to find the memory address in which a particular variable is stored. In C++, it is also used to create a reference.

C++ `

&var_name;

`

**Dot Operator(.)

Dot operator is used to access members of structure variables or class objects using their object names.

C++ `

obj . member;

`

**Arrow Operator

Arrow operator is used to access the variables of classes or structures through its pointer.

C++ `

sptr -> member;

`

**Casting Operators

Casting operators are used to convert the value of one data type to another data type. For example, for an integer value x:

C++ `

(float)x static_cast(x)

`

When there are multiple operators in a single expression, operator precedence and associativity decide in which order and which part of expression are calculate. Precedency tells which part of expression should be calculate first and associativity tells which direction to solve when same precedency operators are in expression.