Operators in C++ with Example

Posted on

Operators in C++

Operators are particular sort of features, that takes a number of arguments and produces a brand new worth. For instance : addition (+), substraction (-), multiplication

and so forth, are all operators. Operators are used to carry out numerous operations on variables and constants.

operators in cpp - Operators in C++ with Example


Forms of operators

  1. Task Operator
  2. Mathematical Operators
  3. Relational Operators
  4. Logical Operators
  5. Bitwise Operators
  6. Shift Operators
  7. Unary Operators
  8. Ternary Operator
  9. Comma Operator

Task Operator ( = )

Operates ‘=’ is used for task, it takes the right-hand aspect (known as rvalue) and duplicate it into the left-hand aspect (known as lvalue). Task operator is the one operator which might be overloaded however can’t be inherited.


Mathematical Operators

There are operators used to carry out primary mathematical operations. Addition (+) , subtraction (-) , diversion (/) multiplication

and modulus (%) are the essential mathematical operators. Modulus operator can’t be used with floating-point numbers.

C++ and C additionally use a shorthand notation to carry out an operation and task at identical sort. Instance,


Relational Operators

These operators set up a relationship between operands. The relational operators are : lower than (<) , grater thatn (>) , lower than or equal to (<=), greater than equal to (>=), equal (==) and never equal (!=).

You will need to discover that task operator is (=) and there’s a relational operator, for equal (==). These two are completely different from one another, the task operator assigns the worth to any variable, whereas equal operator is used to match values, like in if-else situations, Instance


Logical Operators

The logical operators are AND (&&) and OR (||). They’re used to mix two completely different expressions collectively.

If two assertion are related utilizing AND operator, the validity of each statements might be thought of, but when they’re related utilizing OR operator, then both of them have to be legitimate. These operators are largely utilized in loops (particularly whereas loop) and in Determination making.


Bitwise Operators

There are used to vary particular person bits right into a quantity. They work with solely integral information varieties like char, int and lengthy and never with floating level values.

  • Bitwise AND operators &
  • Bitwise OR operator |
  • And bitwise XOR operator ^
  • And, bitwise NOT operator

They can be utilized as shorthand notation too, & = , |= , ^= , = and so forth.


Shift Operators

Shift Operators are used to shift Bits of any variable. It’s of three varieties,

  1. Left Shift Operator <<
  2. Proper Shift Operator >>
  3. Unsigned Proper Shift Operator >>>

Unary Operators

These are the operators which work on just one operand. There are numerous unary operators, however increment ++ and decrement --operators are most used.

Different Unary Operators : deal with of &, dereference *, new and delete, bitwise not , logical not !, unary minus - and unary plus +.


Ternary Operator

The ternary if-else ? : is an operator which has three operands.


Comma Operator

That is used to separate variable names and to separate expressions. In case of expressions, the worth of final expression is produced and used.

Instance :