# C++ Programming Tutorial

## Operators in C++.

The operator is a symbol that tells the compiler to perform any mathematical and logical operation.

Such symbols which are used to perform any type of mathematical and logical operation. They are called operators in C++ programming. In programming, operators are used to manipulating data and variables.

Operators work together with one or more variables, constants, or operands. Variable, constant, operands, function, and operators merge all these together to form an expression.

### Types of operators in C++ programming.

In C++ programming there are lots of built-in operators that have been divided into different types.

Arithmetic Operators.

Assignment Operators.

Relational Operators.

Logical Operators.

Conditional Operators (Ternary Operators).

Bitwise Operators.

Increment / Decrement Operators.

Special Operators.

### 1.Arithmetic Operators

Arithmetic operators are the most basic operators. These operators are used to perform mathematical operations such as Addition (addition), Subtraction (subtraction), Multiplication (multiplication), Division (division / division), Modulus division (modulus division).

Operators | Explanation |
---|---|

+ (Addition) |
It adds two operands. |

- (Subtraction) |
This removes the left operand from the right operand. |

* (Multiplication) |
It multiplies the two operands. |

/ (Division) |
It divides left operand by the right operand. |

% (Modulus) |
It extracts the remainder by dividing left operand by the right operand. |

#include <iostream> using namespace std; int main() { int number1,number2,res; cout<<"\n Enter first number:"; cin>>number1; cout<<"\n Enter second number:"; cin>>number2; res=number1+number2; cout<<"\n Sum of two numbers : "<<res; res=number1-number2; cout<<"\n Subtraction of two numbers : "<<res; res=number1*number2; cout<<"\n Multiplication of two numbers : "<<res; res=number1/number2; cout<<"\n Division of two numbers : "<<res; res=number1%number2; cout<<"\n Remainder of two numbers : "<<res; return 0; }

**Output:**

Enter first number:5 Enter second number:3 Sum of two numbers : 8 Subtraction of two numbers : 2 Multiplication of two numbers : 15 Division of two numbers : 1 Remainder of two numbers : 2

### 2.Assignment Operators

The assignment operator is used to assign a value to a variable. Some operators come in the list of these operators, whose list is given in the table below. For example x=x+y same as x+=y.

Operators | Examples |
---|---|

= (assignment) |
c = a + b |

+= (add assignment) |
c += a same as c = c + a |

-= (subtract assignment) |
c -= a same as c = c - a |

*= (multiply assignment) |
c *= a same as c = c * a |

/= (divide assignment) |
c /= a same as c = c / a |

%= (modulus assignment) |
c %= a same as c = c % a |

&= (AND assignment) |
c &= a same as c = c & a |

|= (OR assignment) |
c |= a same as c = c | a |

^= (XOR assignment) |
c ^= a same as c = c ^ a |

<<= (Left Shift assignment) |
c <<= a same as c = c << a |

>>= (Right Shift assignment) |
c >>= a same as c = c >> a |

#include <iostream> using namespace std; int main() { int number1,number2; cout<<"\n Enter first number:"; cin>>number1; cout<<"\n Enter second number:"; cin>>number2; number1+=number2; cout<<"\n Sum of two numbers : "<<number1; number1-=number2; cout<<"\n Subtraction of two numbers : "<<number1; number1*=number2; cout<<"\n Multiplication of two numbers : "<<number1; number1/=number2; cout<<"\n Division of two numbers : "<<number1; number1%=number2; cout<<"\n Remainder of two numbers : "<<number1; number2 &= 2; cout<<"\nvalue of number2 &= 2 is "<< number2<<endl; number2 |= 2; cout<<"value of 'number2 |= 2' is "<< number2<<endl; number2 ^= 2; cout<<"value of number2 ^= 2 is "<< number2<<endl; number2 <<= 2; cout<<"value of number2 <<= 2 is "<< number2<<endl; number2 >>= 2; cout<<"value of number2 >>= 2 is "<< number2<<endl; return 0; }

**Output:**

Enter first number:5 Enter second number:3 Sum of two numbers : 8 Subtraction of two numbers : 5 Multiplication of two numbers : 15 Division of two numbers : 5 Remainder of two numbers : 2 value of number2 &= 2 is 2 value of 'number2 |= 2' is 2 value of number2 ^= 2 is 0 value of number2 <<= 2 is 0 value of number2 >>= 2 is 0 Enter first number:20 Enter second number:12 Sum of two numbers : 32 Subtraction of two numbers : 20 Multiplication of two numbers : 240 Division of two numbers : 20 Remainder of two numbers : 8 value of number2 &= 2 is 0 value of 'number2 |= 2' is 2 value of number2 ^= 2 is 0 value of number2 <<= 2 is 0 value of number2 >>= 2 is 0

### 3.Relational Operators

Relational Operator is used to compare the value of two operands, hence it is also called a Comparison operator. In the table below, lists of Relational Operators are given.

Operator | Description |
---|---|

== |
This operator is called equal to the operator. This operator is used to equal check two values. If both values are equal, then it returns true. |

!= |
This operator is called Not equal to the operator. It is used to check that two operands are not equal. Meaning that this operator is used to check the value of two operands, if the value of both operands is not equal then it returns true. |

> |
This operator is called Greater than the operator. It is used to check the value of the first operand greater than the value of the second operand. If the value of the first operand is greater than the value of the second operand, then it returns true like (5> 2) return true. |

< |
This operator is called Less than the operator. It is used to check the value of the first operand less than the value of the second operand. If the value of the first operand is smaller than the value of the second operand, then it returns true such as (3 <4) return true. |

>= |
This operator is called Greater than equal to the operator. It is used to check the value of the first operand greater than and equal to the value of the second operand. If the value of the first operand is greater than or equal to the value of the second operand, then it returns true as (5> = 5) return true. |

<= |
This operator is called Less than equal to the operator. It is used to check the value of the first operand less than the value of the second operand. If the value of the first operand is smaller than or equal to the value of the second operand, it returns true such that (5 <= 5) return true. |

#include <iostream> using namespace std; int main(){ int number1, number2; cout<<"\n Enter first number : "; cin>>number1; cout<<"\n Enter second number : "; cin>>number2; if(number1< number2) { cout<<" number1 is less than number2"<<endl; } else { cout<<" number1 is greater than number2"<<endl; } if(number1<= number2) { cout<<" number1 is less than number2"<<endl; } else{ cout<<" number1 is greater than number2"<<endl; } if(number1> number2) { cout<<" number1 is greater than number2"<<endl; } else { cout<<" number1 is less than number2"<<endl; } if(number1>= number2) { cout<<" number1 is greater than number2"<endl; } else { cout<<" number1 is less than number2"<<endl; } if(number1== number2) { cout<<" number1 is equal to number2"<<endl; } else { cout<<" number1 is not equal to number2"<<endl; } return 0; }

**Output:**

Enter first number : 31 Enter second number : 36 number1 is less than number2 number1 is less than number2 number1 is less than number2 number1 is less than number2 number1 is not equal to number2 Enter first number : 31 Enter second number : 31 number1 is greater than number2 number1 is less than number2 number1 is less than number2 number1 is greater than number2 number1 is equal to number2

### 4.Logical Operators.

Logical operators are used to combining two expressions and a condition. These operators are used to perform logical operations.

Operators | Explanation |
---|---|

&& (logical &&) |
If both the conditions are true then it will return true.for e.g. (5 <6) && (6> 5) |

|| (logical OR) |
If either one is true, then it will return true. For e.g. (5 <6) || (6> 5) |

! (logical not) |
If the condition is true, it makes it false.for e.g. ! ((5 <6) && (6> 5)) ! ((5 <6) || (6> 5)) |

#include <iostream> using namespace std; int main() { int number1,number2,number3,number4; cout<<"\n Enter first number :"; cin>>number1; cout<<"\n Enter second number :"; cin>>number2; cout<<"\n Enter third number :"; cin>>number3; cout<<"\n Enter fourth number :"; cin>>number4; if((number1<number2) && (number3>number4)) { cout<<"Condition is true ( AND operator)."<<endl; } else { cout<<"Condition is false ( AND operator)."<<endl; } if((number1<number2) || (number3>number4)) { cout<<"Condition is true ( OR operator)."<<endl; } else { cout<<"Condition is false ( OR operator)."<<endl; } if(!((number1<number2) && (number3>number4))) { cout<<"Condition is true ( NOT operator)."<<endl; } else { cout<<"Condition is false ( NOT operator)."<<endl; } return 0; }

**Output:**

Enter first number :10 Enter second number :20 Enter third number :15 Enter fourth number :8 Condition is true ( AND operator). Condition is true ( OR operator). Condition is false ( NOT operator).

### 5.Conditional Operators (Ternary Operators)

Conditional operators are used to checking the condition. This operator has only two options TRUE and FALSE. If the given condition satisfies, then TRUE will return otherwise FALSE will be returned.

**Syntax of conditional operators.**

Condition ? True Expression : False Expression;

#include <iostream> using namespace std; int main() { int number; cout<<"\n Enter any number :"; cin>>number; ( number%2==0) ? cout<<"\n It is Even number" : cout<<"\n It is Odd number" ; return 0; }

**Output:**

Enter any number :31 It is Odd number Enter any number :36 It is Even number

### 6.Bitwise Operators.

Bitwise operator is used to manipulating BIT level data. This operator is used for shifting from right to left and left to right bit. The bitwise operator does not apply to float and double data type.

The full form of Bit is a binary digit which is 0 and 1. The bitwise operator is calculated at 0 and 1. Whenever we manipulate the decimal number by the bitwise operator, the processor first converts it in the form of 0 and 1 and then calculates it.

Operator | Description |
---|---|

& |
Bitwise AND |

| |
Bitwise OR |

^ |
Bitwise exclusive OR |

<< |
Left Shift |

>> |
Right Shift |

### Bitwise operator &, |, ^

p | q | p & q | p | q | p ^ q |
---|---|---|---|---|

0 |
0 |
0 |
0 |
0 |

0 |
1 |
0 |
1 |
1 |

1 |
0 |
0 |
1 |
1 |

1 |
1 |
1 |
1 |
0 |

**Operation on AND(p&q)**

Decimal Value Binary Value If p = 20, q = 12, then 20 0 0 0 1 0 1 0 0 12 0 0 0 0 1 1 0 0 -------------------------- 4 0 0 0 0 0 1 0 0 --------------------------

**Operation on OR(p|q)**

Decimal Value Binary Value If p = 20, q = 12, then 20 0 0 0 1 0 1 0 0 12 0 0 0 0 1 1 0 0 -------------------------- 28 0 0 0 1 1 1 0 0 --------------------------

**Operation on XOR(a^b)**

Decimal Value Binary Value If a = 20, b = 12, then 20 0 0 0 1 0 1 0 0 12 0 0 0 0 1 1 0 0 -------------------------- 24 0 0 0 1 1 0 0 0 --------------------------

### Binary Left Shift( << ) and Right Shift( >> )

Left Shift (<<) for e.g. a = 20; / * 0001 0100 * / a << Shifts every binary number in binary value of numeric value in 2 with 2 binary numbers left side. For e.g.a = 20; / * 0001 0100 * / So this means 0101 0000 will be 80.

Right Shift (>>) for e.g. a = 20; / * 0001 0100 * / This is completely opposite from Left shift. Right Shift a >> shifts every binary number from the right side to 2 binary numbers in binary value of numeric value in 2. for e.g.a = 20; / * 0001 0100 * / So its 0000 0101 means 5.

### Complement Operator (~)

This operator does all the bit reverse.

This operator reduces 0 to 1 and 0 to 0.

**Operation on Complement( ~ )**

Decimal Value Binary Value ~12 0 0 0 0 1 1 0 0 -------------------------- 243 1 1 1 1 0 0 1 1 -------------------------- Why did 243 come here instead of Output -13?

### 2's Complement of 243 -(reverse of 243 in binary + 1)

Operation on 2's Complement( ~ )

Decimal Value Binary Value 2's Complement

243 1111 0011 -(0000 1100+1) = -(0000 1101) = -13

#include <iostream> using namespace std; int main() { int number1,number2,result; cout<<"\n Enter first number:"; cin>>number1; cout<<"\n Enter second number:"; cin>>number2; result=number1&number2; cout<<"value of result(number1&number2) is "<<result<<endl; result=number1|number2; cout<<"value of result(number1|number2) is "<<result<<endl; result=number1^number2; cout<<"value of result(number1^number2) is "<<result<<endl; result=number1<<2; cout<<"value of result(number1<<2) is "<<result<<endl; result=number1>>2; cout<<"value of result(number1>>2) is "<<result<<endl; cout<<"value of result(~number1) is "<<~number1<<endl; return 0; }

**Output:**

Enter first number:20 Enter second number:12 value of result(number1&number2) is 4 value of result(number1|number2) is 28 value of result(number1^number2) is 24 value of result(number1<<2) is 80 value of result(number1>>2) is 5 value of result(~number1) is -21 Enter first number:31 Enter second number:36 value of result(number1&number2) is 4 value of result(number1|number2) is 63 value of result(number1^number2) is 59 value of result(number1<<2) is 124 value of result(number1>>2) is 7 value of result(~number1) is -32

### 7.Increment / Decrement Operators.

The increment / decrement operator is used to increase and decrease the value of the variable once. For example, if the variable already has a 5 value store, then its value will be reduced to 6 by increment operator and its value will be 4 by decrement operator.

There are two types of increase / decrement operator.

Pre increment.

Pre decrement.

Post increment.

Post decrement.

### Pre increment / decrement operator.

In the pre-increment/decrement operator, first, the value increase and decrease occur and then the further calculation is performed.

**Syntax of pre increment / decrement operator.**

++ (variable name), – – (variable name)

### Post increment / decrement operator.

In the post-increment/decrement operator, the calculation is first performed and then the value change occurs. Post means later. There is a value increase and decrease after the calculation is performed.

**Syntax of post increment / decrement operator.**

(variable name) ++, (variable name) – –

#include <iostream> using namespace std; int main() { int x; cout<<"\n Enter x value :"; cin>>x; cout<<"Print Value of Pre-increment : "<<++x<<endl; cout<<"Value of x : "<<x<<endl; cout<<"Print Value of Pre-decrement : "<<--x<<endl; cout<<"Value of x : "<<x<<endl; cout<<"Print Value of Post increment : "<<x++<<endl; cout<<"Value of x : "<<x<<endl; cout<<"Print Value of Post decrement : "<<x--<<endl; cout<<"Value of x : "<<x<<endl; return 0; }

**Output:**

Enter x value :31 Print Value of Pre-increment : 32 Value of x : 32 Print Value of Pre-decrement : 31 Value of x : 31 Print Value of Post increment : 31 Value of x : 32 Print Value of Post decrement : 32 Value of x : 31