C++ Program to Demonstrate the Assignment Operator

Last updated on:

Like Arithmetic Operator, in this tutorial, we will learn about the Assignment Operators i.e. how to demonstrate the Assignment Operator using C++.

So, here’s some quick information related to the program which can help you to know better, i.e. What actually it is?

Quick Info:πŸ’‘

β†ͺ Assignment operator is an operator used to assign the value to the variables.

β†ͺ The basic assignment operator includes:

  • += (Combination of β€˜+’ and β€˜=’ operator)
  • -= (Combination of β€˜-β€˜ and β€˜=’ operator)
  • *=(Combination of β€˜*’ and β€˜=’ operator)
  • /= (Combination of β€˜/’ and β€˜=’ operator)
  • %= (Combination of β€˜%’ and β€˜=’ operator)

β†ͺ Steps to perform the above task:

  • First, input the value of the operands.
  • Second, use the different assignment operators to demonstrate it.
  • Third, display the result on the output screen.

β†ͺ It can be done using different ways:

  • Using a Simple Method
  • Using a Class
  • Using a Function

Demonstrate the Assignment Operator Using Simple Method

Here’s a simple method that demonstrates the Assignment operator.

Example:

#include<iostream.h>
#include<conio.h>
void main()
{
int number;
cout<<"Enter the value of number:";
cin>>number;
//Assigning value by adding 5 to number
number+=5;        //number=number+5;
cout<<"Number after += is: "<<number;
//Assigning value by subtracting 5 from number
number-=5;        //number=number-5;
cout<<"Number after -= is: "<<number;
//Assigning value by multiplying 5 to number
number*=5;        //number=number*5;
cout<<"Number after *= is: "<<number;
//Assigning value by dividing 5 to number
number/=5;        //number=number/5;
cout<<"Number after /= is: "<<number;
//Assigning value by dividing 5 to number
number%=5;        //number=number%5;
cout<<"Number after %= is: "<<number;
getch();
}
//Output:
Enter the value of number:30
Number after += is: 35
Number after -= is: 30
Number after *= is: 150
Number after /= is: 30
Number after %= is: 0
Assignment Operator

Working:

Flow of a program:

Assignment operator working

Demonstrate the Assignment Operator Using a Class

A class is a user-defined data type which makes C++ an object-oriented language.

We create a class with two functions getdata and assignment_op. Function getdata is used to get two integers from a user, and function assignment_op operates and displays the result.

So, here I will show you, how to demonstrate the assignment operators using class.

Example:

#include<iostream.h>
#include<conio.h>
class assignment
{
public:
int num;
void getdata()
{
cout<<"Enter the value of num:=";
cin>>num;
}
void assignment_op()
{
cout<<"Number after += is: "<<(num+=5)<<endl;
cout<<"Number after -= is: "<<(num-=5)<<endl;
cout<<"Number after *= is: "<<(num*=5)<<endl;
cout<<"Number after /= is: "<<(num/=5)<<endl;
cout<<"Number after %= is: "<<(num%=5)<<endl;
}
};
void main()
{
assignment obj;
obj.getdata();
obj.assignment_op();
getch();
}
//Output:
Enter the value of number:20
Number after += is: 25
Number after -= is: 20
Number after *= is: 100
Number after /= is: 20
Number after %= is: 0
Assignment Operator Class Method

Working:

Flow of a program:

Assignment operator class working

Demonstrate the Assignment Operator Using a Function

A function is a group of statements which perform a specific task.

Actually, it is a self-contained block of statements which performs its task when get executed.

For operations, first, create functions for different assignment operators and pass the arguments for integer.

Example:

#include<iostream.h>
#include<conio.h>
int fun1(int a){
cout<<"Number after += is:=;
return a+=2;
}
int fun2(int a){
cout<<"Number after -= is:=;
return a-=2;
}
int fun3(int a){
cout<<"Number after *= is:=;
return a*=2;
}
int fun4(int a){
cout<<"Number after /= is:=;
return a/=2;
}
int fun5(int a){
cout<<"Number after %= is:=;
return a%=2;
}
void main()
{
int number;
cout<<"Enter the number";
cin>>number;
cout<<fun1(number)<<endl;
cout<<fun2(number)<<endl;
cout<<fun3(number)<<endl;
cout<<fun4(number)<<endl;
cout<<fun5(number)<<endl;
getch();
}
//Output:
Enter the value of number:10
Number after += is: 12
Number after -= is: 8
Number after *= is: 20
Number after /= is: 5
Number after %= is: 0
Assignment operator function

Working:

Flow of a program:

Assignment operator function working

From the above examples, we understood that there are several ways to write a single program using different ways.

Solving a program with different methods is quite interesting. Right?

Well, I hope it really helped you to know C++ better.

And, if you liked this stay with me to get more such exciting codes to explore more in C++.

Comments

Leave a Reply

Your email address will not be published. Required fields are marked *