Assignment Operators Examples in C++


Description

Assignment operators in C++ are operators that are used to assign a value to a variable. C++ provides several assignment operators, which include:

  • Simple Assignment Operator (=): This operator assigns a value to a variable.
  • Addition Assignment Operator (+=): This operator adds a value to a variable and assigns the result to the variable.
  • Subtraction Assignment Operator (-=): This operator subtracts a value from a variable and assigns the result to the variable.
  • Multiplication Assignment Operator (*=): This operator multiplies a variable by a value and assigns the result to the variable.
  • Division Assignment Operator (/=): This operator divides a variable by a value and assigns the result to the variable.
  • Modulus Assignment Operator (%=): This operator returns the remainder of a variable divided by a value and assigns the result to the variable.

Assignment operators can be used with various data types in C++, including integers, floating-point numbers, and characters. These operators are used extensively in programming to simplify code by performing arithmetic and assignment in a single step.

Source Code Example : 1

#include<iostream>
using namespace std;
int main()
{
    int x=10;
    x+=5;
    cout<<"\nValue of x: "<<x;
    return 0;
}
To download raw file Click Here

Output

Value of x: 15

Source Code Example : 2

#include<iostream>
using namespace std;
int main()
{
    int x=10;
    x-=5;
    cout<<"\nValue of x: "<<x;
    return 0;
}
To download raw file Click Here

Output

Value of x: 5

Source Code Example : 3

#include<iostream>
using namespace std;
int main()
{
    int x=10;
    x*=5;
    cout<<"\nValue of x: "<<x;
    return 0;
}
To download raw file Click Here

Output

Value of x: 50

Source Code Example : 4

#include<iostream>
using namespace std;
int main()
{
    int x=10;
    x/=5;
    cout<<"\nValue of x: "<<x;
    return 0;
}
To download raw file Click Here

Output

Value of x: 2

Source Code Example : 5

#include<iostream>
using namespace std;
int main()
{
    int x=10;
    x%=5;
    cout<<"\nValue of x: "<<x;
    return 0;
}
To download raw file Click Here

Output

Value of x: 0

Source Code Example : 6

#include<iostream>
using namespace std;
int main()
{
    int x=10;
    x^=2;
    cout<<"\nValue of x: "<<x;
    return 0;
}
To download raw file Click Here

Output

Value of x: 8

Program List


Flow Control

IF Statement Examples


Switch Case


Goto Statement


Break and Continue


While Loop


Do While Loop


For Loop


Friend Function in C++


String Examples


Array Examples


Structure Examples


Structure & Pointer Examples


Structure & Functions Examples


Enumeration Examples


Template Examples


Functions


Inheritance Examples

Hierarchical Inheritance


Hybrid Inheritance


Multilevel Inheritance


Multiple Inheritance


Single Level Inheritance


Class and Objects

Constructor Example


Destructor Example


Operator Overloading Example


Operator and Function Example


List of Programs


Pointer Examples


Memory Management Examples


Pointers and Arrays


Virtual Function Examples