Assignment Operator In C++ with example

Assignment Operators ?

An assignment operator is the operator used to assign a new value to a variable, property, event or indexer element in C# programming language. Assignment operators can also be used for logical operations such as bitwise logical operations or operations on integral operands and Boolean operands.


The copy assignment operator, often just called the "assignment operator", is a special case of assignment operator where the source (right-hand side) and destination (left-hand side) are of the same class type.

Unlike in C++, assignment operators in C# cannot be overloaded directly, but the user-defined types can overload the operators like +, -, /, etc. This allows the assignment operator to be used with those types.

The default version performs a memberwise copy, where each member is copied by its own copy assignment operator (which may also be programmer-declared or compiler-generated).


Assignment Operators

Operator Description
= Assign
+= Increments, then assigns
-= Decrements, then assigns
*= Multiplies, then assigns
/= Divides, then assigns
%= Modulus, then assigns
<<= Left shift and assigns
>>= Right shift and assigns
&= Bitwise AND assigns
^= Bitwise exclusive OR and assigns
|= Bitwise inclusive OR and assigns

Example 1:



Example 2:

Example 3:



Output :

  5

Example 4:



Output :

  
      First Distance : F: 11 I:10
      Second Distance :F: 5 I:11
      First Distance :F: 5 I:11