Broad Network


Assignment Operators in C++

C++ Operators – Part 1

Forward: This is the first part of a series in which I explain C++ operators.

By: Chrysanthus Date Published: 23 Aug 2012

Introduction

Addition and subtraction symbols are examples of operators in C++. C++ has many other operators that do not have similarities with mathematics. In fact you should learn C++ operators as they are given without making similarities with mathematics. Making similarities can be misleading. This is the first part of a series in which I explain C++ operators. Take things as I give you and avoid making similarities with mathematics.

If you are an old programmer, you can read the series in any order or you can read just the part, which is related to a problem you currently have at your job site. If you are new to programming or you studied programming and have not been practicing, then you should read the whole series.

You need to have basic knowledge in C++ in order to understand this series. If you do not have basic knowledge in C++, then read the series in this blog whose first part is titled, “Getting Started with C++”. To arrive at the series, just type the title and my name, Chrys in the Search box of this page and click Search.

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.

In this part of the series, we look at assignment operators in C++. An example of such an operator is =.

Operand
An Operand is an identifier or a literal (value) associated with an operator. Consider,

    myIdent = 30;  //this ID should have been declared as an int

myIdent is a left operand and 30 is a right operand to =. = is an example of an operator called, the assignment operator, not the equal operator (details below).

Consider:

      myIdent && hisIdent && herIdent

There are three operands in the above expression. So, you can talk of the first (myIdent), second (hisIdent) and third operands (herIdent). &&, that is 2 ampersands form another operator; we shall talk about it later.

Binary and Unary Operators
A binary operator needs two operands to work with: one on its left and one on its right. A unary operator needs only one operand to work with, placed on its left or on its right (see later).

Assignment Operators
Assignment operators are:

=  *=  /=  %=  +=  -=  >>=  <<=  &=  ˆ=  |=

We look at each of these operators in this part of the series.

Simple Assignment (=)
The simple assignment operator is, =. The following statement illustrates an example:

    int myInt = 35;

We say the integer 35 is assigned to the object, identified by myInt. The left operand to = is myInt. The right operand is 35. The left and right operands should be of the same type.

For all assignment operators the operation is from right to left; that is, from the right operand to the left operand. If you are trying to make similarity with mathematics, then the previous sentence (right to left) may not make sense; avoid making similarity with mathematics. We shall see many examples of right to left and left to right operations as we go along in the series.

For simple assignment, the right operand may be another identifier or even a function call that returns a value.

E1 op= E2 Expressions
In the following generalized statement, E1 is an object identifier, op is an operator and E2 is another object identifier; = is the simple assignment operator:

    E1 op= E2

This generalized statement that involves the simple assignment operator is equivalent to,

    E1 = E1 op E2

The rest of the assignment operators in the list above follow this generalized rule as explained below. The rest of the assignment operators are each made up of an operator you might already know and the simple assignment operator.

The *= Operator
Consider the following statement:

    int int1 = int1 * int2;

Here, int1 and int2 are object identifiers. Note that int1 is found in both the left operand and the right operand (int1 * int2), of the simple assignment operator. We also have the multiplication operator, *. In the above statement the result of multiplying int1 by int2 is assigned to int1. The statement can be re-written as:

    int int1 *= int2;

Also, int2 can be replaced by a number as in,

    int int1 *=3;

The /= Operator
Consider the following statement:

    int int1 = int1 / int2;

Here, int1 and int2 are object identifiers. Note that int1 is found in both the left operand and the right operand. We also have the division operator, /. In the above statement the result of dividing int1 by int2 is assigned to int1. The statement can be re-written as:

    int int1 /= int2;

Also, int2 can be replaced by a number as in,

    int int1 /=4;

The following program illustrates this:

#include <iostream>
using namespace std;

    int main()
        {
            int int1 = 20;
            int int2 = 4;

            int1 /= int2;
            cout << int1;

            return 0;
       }

The %= Operator
Consider the following statement:

    int int1 = int1 % int2;

Here, int1 and int2 are object identifiers. Note that int1 is found in both the left operand and right operand. We also have the modulus operator, %. In the above statement the remainder of dividing int1 by int2 is assigned to int1. The statement can be re-written as:

    int int1 %= int2;

The += Operator
Consider the following statement:

    int int1 = int1 + int2;

Here, int1 and int2 are object identifiers. Note that int1 is found in both the left operand and the right operand of the simple assignment operator. We also have the addition operator, +. In the above statement the result of adding int1 to int2 is assigned to int1. The statement can be re-written as:

    int int1 += int2;

The -= Operator
Consider the following statement:

    int int1 = int1 - int2;

Here, int1 and int2 are object identifiers. Note that int1 is found in both the left operand and the right operand of the simple assignment operator. We also have the subtraction operator, -. In the above statement the result of subtracting int2 from int1, is assigned to int1. The statement can be re-written as:

    int int1 -= int2;

Bitwise Assignment Operators
>>=  <<=  &=  ˆ=  and |= are known as bitwise assignment operators. These are usually used by electronic programmers. They are not normally used by the programmers of business organizations and for common projects. I intend to address them in a separate series.

With that we have come to the end of assignment operators. See you in the next part of the series.

Chrys

Related Links

C++ Taking the Bull by the Horns
C++ Just After the Basics
Object Oriented Programming in C++
C++ Operators
C++ Templates
Exception Handling in C++
Container Library Sequences in C++ Simplified
Associative Container in C++ Simplified
String in C++ Standard Library Simplified
Date and Time in C++ Simplified
Advanced Course
Storage Duration in C++
Scopes in C++
Function and Operator Overloading in C++
Specifiers in C++
Some Features of C++ Entities
C++ Preprocessing Directives
Writing a C++ Container
C++ Namespace
Writing a C++ Application
More Related Links
Relational Database and Sybase
Windows User Interface
Computer Programmer – A Jack of all Trade – Poem
C++ Course
NEXT

Comments

Become the Writer's Fan
Send the Writer a Message