Broad Network


Arithmetic Operators in C++

C++ Operators – Part 2

Forward: In this part of the series we look at Arithmetic Operators in C++.

By: Chrysanthus Date Published: 23 Aug 2012

Introduction

This is part 2 of my series, C++ Operators. In this part of the series we look at Arithmetic Operators in C++.

Note: If you cannot see the code or if you think anything is missing (broken link, image absent), just contact me at forchatrans@yahoo.com. That is, contact me for the slightest problem you have about what you are reading.

Addition Operator
Read and try the following code. The explanation is given below:

#include <iostream>
using namespace std;

int main()
    {
        int id1 = 20;
        int id2 = 30;
        
        int id3 = id2 + id1;
        cout << id3;

        return 0;
    }

20 is kept in the object identified by id1. 30 is kept in the object identified by id2. In the third statement of the block of the main function, C++ takes the content of id2 and adds it to the content of id1, then it puts the result as content for the object of the newly declared identifier, id3. This addition is done without affecting or changing the contents of id2 and id1. The addition is done in a different area of memory.

Subtraction Operator
Read and try the following code. The explanation is given below:

Code example:

#include <iostream>
using namespace std;

int main()
    {
        int id1 = 20;
        int id2 = 30;
        
        int id3 = id2 - id1;
        cout << id3;
        
        return 0;
    }

20 is kept in the object identified by id1. 30 is kept in the object identified by id2. In the third statement of the block of the main function, C++ takes the content of id2 and subtracts from it the content of id1, then it puts the result as content for the object of the newly declared identifier, id3. This subtraction is done without affecting or changing the contents of id2 and id1. The subtraction is done in a different area of memory.

Multiplication Operator
Read and try the following code. The explanation is given below:

#include <iostream>
using namespace std;

int main()
    {
        int id1 = 20;
        int id2 = 30;
        
        int id3 = id2 * id1;
        cout << id3;
        
        return 0;
    }

Note that the multiplication operator is * and not X. Here * is the multiplication operator and not the dereference operator. 20 is kept in the object identified by id1. 30 is kept in the object identified by id2. In the third statement of the block of the main function, C++ takes the content of id2 and multiplies it with the content of id1, then it puts the result as content for the object of the newly declared identifier, id3. This multiplication is done without affecting or changing the contents of id2 and id1. The multiplication is done in a different area of memory.

Division Operator
Read and try the following code. The explanation is given below:
Code example:

#include <iostream>
using namespace std;

int main()
    {
        int id1 = 3;
        int id2 = 15;
    
        int id3 = id2 / id1;
        cout << id3;
        
        return 0;
    }

Note that the division operator is, / . 3 is kept in the object identified by id1. 15 is kept in the object identified by id2. In the third statement of the block of the main function, C++ takes the content of id2 and divides it by the content of id1, then it puts the result as content for the object of the newly declared identifier, id3. This multiplication is done without affecting or changing the contents of id2 and id1. The multiplication is done in a different area of memory.

Modulus Operator
The modulus operator divides the first operand by the second operand and returns the remainder. Read and try the following code:

#include <iostream>
using namespace std;

int main()
    {
        int id1 = 17;
        int id2 = 12;
        
        int id3 = id1 % id2;
        
        cout << id3;
        
        return 0;
    }

The Modulus operator is the percentage sign. For the third statement in the main function block above, C++ takes the content of id1 and divides it by the content of id2. The remainder of the division is put in the object of id3. The division is done in a different area of memory.

So there are 5 arithmetic operators that are:

+  - * / %

Arithmetic operators operate from left to right; that is from the left operand to the right operand.

That is it for arithmetic operators. We take a break here and continue in the next part of the series.

Chrys

Related Courses

C++ Course
Relational Database and Sybase
Windows User Interface
Computer Programmer – A Jack of all Trade – Poem
NEXT

Comments

Become the Writer's Fan
Send the Writer a Message