Assignment Operators in C++

Assignment Operators in C++
Techiio-author
Written by Sagar RabidasFebruary 6, 2022
8 min read
C++
0 VIEWS 0 LIKES 0 DISLIKES SHARE
0 LIKES 0 DISLIKES 0 VIEWS SHARE
Techiio-author
Sagar Rabidas

Software Developer

Let's start the discussion with Assignment Operators in C++

Assignment Operators in C++

Right here, let us start with the aid of understanding approximately mission operators in c++. Because the call already suggests, those operators assist in assigning values to variables. We talk about these concerning operators and operands. These operators assist us in allocating a selected fee to the operands.

The primary easy mission operator is ‘=’. We ought to make sure that both the left and proper facets of the operator should have the same records type. We've extraordinary tiers of operators. Let us find out about each of those in c++. Undertaking operators are part of binary operators. Examples for these are: =, +=, -=, *=, /=, %=. Let us find out about each of these with examples.

Assignment Operators

There are three levels of operators.

  • Unary Operators
  • Binary Operators
  • Ternary Operators

Assignment operators are a part of binary operators.

Examples for these are: =, +=, -=, *=, /=, %=. Let us learn about each of these with examples.

Examples #1

Let us start with the first example with the ‘=’ operator as given below.

Code:

#include <iostream>
using namespace std;
int main()
{
int a,b;
char c;
float d;
a=10;
b=10.5;
c='R';
d=5.85;
cout<<" Value of a is: "<<a<<endl;
cout<<" Value of b is: "<<b<<endl;
cout<<" Value of c is: "<<c<<endl;
cout<<" Value of d is: "<<d<<endl;
}

Output:-

blogpost

So, as an example, we can take into account that the assignment operator ‘=’ is just going to assign values based on the statistics type the usage of the operands and variables. Above, we can see that the cost b that is given the facts type of int, offers best the price till the decimal factor. Simplest if we deliver the statistics kind drift just like the variable d, the complete cost receives displayed. So, we will say that records kind also play a vital position in placing and showing the values of different operands primarily based on their values.

Examples #2

In this example let us learn how ‘+=’ this operand works.

Code:

#include <iostream>
using namespace std;
int main()
{
int a,b,c;
a=10;
b=5;
c=5;
a+=6;
b+=20;
c=c+20;
cout<<" Value of a is: "<<a<<endl;
cout<<" Value of b is: "<<b<<endl;
cout<<" Value of c is: "<<c<<endl;
}

Output:-

blogpost

As observed above, the instance of ways we were given the cost for variable c, is the method of ways the operator ‘+=’ works. As consistent with the operand, first, the left aspect operand is introduced to the left aspect operand and then the very last price is being assigned to the left facet operand. That is how ‘+=’ is used.

Examples #3

Now, let us learn about the operand ‘-=’. This is almost similar to that of ‘+=’. The above operand adds the value, but here it gets subtracted. Let us see an example below.

Code:

#include <iostream>
using namespace std;
int main()
{
int a,b,c;
a=10;
b=5;
c=5;
a-=6;
b-=20;
c=c-4;
cout<<" Value of a is: "<<a<<endl;
cout<<" Value of b is: "<<b<<endl;
cout<<" Value of c is: "<<c<<endl;
}

Output:-

blogpost

Here also we have given example for variable ‘c’ on how the assignment of value is done. First, the left side operand value subtracts the right-hand value from it. In this process, there might be a negative value obtained.

Examples #4

Here let us have an example for the assignment operator *=, /= and %= together.

Code:

#include <iostream>
using namespace std;
int main()
{
int a,b,c;
a=10;
b=20;
c=25;
a*=2;
b/=5;
c%=10;
cout<<" Value of a is: "<<a<<endl;
cout<<" Value of b is: "<<b<<endl;
cout<<" Value of c is: "<<c<<endl;
}

Output:-

blogpost
Now coming to the interpretation of the above example.
  • The value for variable ‘a’ is just by multiplying the right with left operands and value respectively.
  • The value of variable ‘b’ is obtained by dividing the left operand with the right side value. The output is the quotient obtained from that division.
  • The value for variable ‘c’ is obtained by division only. But here the output value is the remainder of the division. As already known the left operand is divided with the right side value/operand.

Examples #5

Now let us look at how operators &=, ^= and |= as shown below.

Code:

#include <iostream>
using namespace std;
int main()
{
int a,b,c;
a=5;
b=6;
c=7;
a&=3;
b^=3;
c|=3;
cout<<" Value of a is: "<<a<<endl;
cout<<" Value of b is: "<<b<<endl;
cout<<" Value of c is: "<<c<<endl;
}

Examples #6

Here, let us learn about the operators <<= and >>= as given below.

Code:

#include <iostream>
using namespace std;
int main()
{
int a,b;
a=5;
b=5;
a>>=1;
b<<=1;
cout<<" Value of a is: "<<a<<endl;
cout<<" Value of b is: "<<b<<endl;
}

C
C++
Assignment Operators
0 VIEWS 0 LIKES 0 DISLIKES SHARE
0 LIKES 0 DISLIKES 0 VIEWS SHARE
Was this blog helpful?
techiio-price-plantechiio-price-plantechiio-price-plantechiio-price-plantechiio-price-plan
You must be Logged in to comment
Code Block
Techiio-logo

Techiio is on the journey to build an ocean of technical knowledge, scouring the emerging stars in process and proffering them to the corporate world.

Follow us on:

Subscribe to get latest updates

You can unsubscribe anytime from getting updates from us
Developed and maintained by Wikiance
Developed and maintained by Wikiance