Operators in CPLUSPLUS language


Operators in CPLUSPLUS
operators

Image credit:xoax.net
Operators in CPLUSPLUS language play vital role by providing accurate and error free computations. The operators are not only valid for CPLUSPLUS but they are also extensively in other programing languages such as Vb.net, C#, Java, Perl, PHP etc.Generally, operators are applied to variables such as operand

Types of operators

There is a franchise of operators that are available for dozens of applications and their proper usages such as

A whole list of operators and their functions are shown below:

Arithmetic operators

+ (addition)
– (subtraction)
* (multiplication)
/ (division)
% (modulus)


Bitwise operators

^ (XOR)
| (OR)
& (AND)
~ (complement)
<< (shift left, insertion to stream) >> (shift right, extraction from stream)


Assignment operator
=



Relational operators

== (equality)
!= (inequality)
> (greater-than)
< (less-than) >= (greater-than-or-equal-to)
<= (less-than-or-equal-to)


Logical operators

! (NOT)
&& (AND)
|| (OR)



Compound assignment operators

+= (addition-assignment)
-= (subtraction-assignment)
*= (multiplication-assignment)
/= (division-assignment)
%= (modulus-assignment)
&= (AND-assignment)
|= (OR-assignment)
^= (XOR-assignment)
<<= (shift-left-assignment) >>= (shift-right-assignment)


?: (conditional)
. (member selection)
.* (member selection with pointer-to-member)
:: (scope resolution)
sizeof (object size information)
typeid (object type information)

Prefix and Postfix Unary operators

    Example
    #include “stdafx.h”
    #include <iostream>
    using namespace std;

    int main() {
    int i = 5;
    // in increment post fix operator, the first value is same and it won’t get changed
    cout << i++ << endl; // now our variable is holding an incremented value,thatvvalue will get printed cout << i << endl; }

Output

5
6

    // ConsoleApplication5.cpp : Defines the entry point for the console application.
    //

    #include “stdafx.h”
    #include <iostream>
    using namespace std;

    int main() {
    int i = 5;
    // in increment pre fix operator, the incremented value os seen straight away
    cout << ++i<< endl; }

Output
6

Another Example

    #include “stdafx.h”
    #include

    using namespace std;

    int main()

    {

    int value = 10;

    cout << "Previous Value = " << value << endl; value -= 2; // same as value=value-1 cout << "New Value = " << value << endl; return 0; }

The operators that we discussed are increment operators. The decrement operators have the same functionality but it decrements the values. Both come under the category of Unary operators because they operate on one operand (i.e variable)

sizeof() operator

In the programming languages C and C++, the unary operator sizeof is used to calculate the size of any datatype, measured in the number of bytes required to represent the type. A byte in this context is the same as an unsigned char, and may be larger than 8 bits, although that is uncommon. The result of sizeof is the size of the type of the expression or parenthesized type-specifier that it precedes, and has an unsigned integral type which is usually denoted by size_t. sizeof can be applied to any datatype, including primitive types such as integer and floating-point types, pointer types, or compound datatypes (unions, structs, or C++ classes).

    #include “stdafx.h”
    #include <iostream>
    using namespace std;

    int main()
    {
    char myArray[20] = “Adeel, Ahmed”;
    cout<< "sizeof array is:"; sizeof(myArray); cut<< sizeof(myArray); cout<

    Relational plus comparison operators in a plate

    The Relational and Comparison operators are also called binary operators because they work with two operands. Their major use is to check whether the if statement is justified.
    Following are the types of operators mostly used in if condition

    == Equal to
    != Not equal to
    < Less than > Greater than
    <= Less than or equal to >= Greater than or equal to

    Logical operators

    The logical operator just checks, whether the criteria of code meets proper standards

    if(!IsPostBack)
    while(! true)
    if(a==b)
    if( (8 == 8) && (5 > 2) ) // true statement with the use of && and operator
    of( (100 == 100) || (2 > 20) ) // true statement as 100=100 and use of || or

    Binary Operators
    Minimum number of operands( variables) required are 2.

    Complete list of C++ operators are here

    Relational Operators
    Both operands of each side of operators must be of same type.

    Example

      #include “stdafx.h”
      #include <iostream>

      using namespace std;

      int main() {
      cout<< "False statement yields 0" < 10) << endl; // yeilds true }

    Operators precedence chart

    image credit:http://en.wikipedia.org/

    The Binary operators
    The name suggests that these operators work with more then one variable or operand. Examples

    a+b,
    c*d,
    a/h;
    g-i;
    etc

    Ternary works on three operands. Let’s see an example

      #include “stdafx.h”
      #include <iostream>

      using namespace std;

      int main() {

      float a, b;
      a = b = 0;

      std::cout << "Your height: "; std::cin >> a;

      std::cout << "n Enter brother's height: "; std::cin >> b;

      std::cout << "Taller: " << ((a > b) ? a : b) << "nSmaller: " << ((a < b) ? a : b) << std::endl; return 0; } }

    Output

    Ternary Operator
    This operator looks killer if you see what it performs without any logic. Let’s see the example

      include “stdafx.h”
      #include <iostream>

      using namespace std;
      int main() {
      int i = 1, j = 2;
      //check the comparison.If i is greater than j then the value of i gets printed otherwise j gets printed
      cout << ( i > j ? i : j ) << " is greater." << endl; }

    Syed Adeel Ahmed
    Syed Adeel Ahmed
    Analyst, Programmer, Educationist and Blogger at Technofranchise
    Computer Systems Engineer from Sir Syed University Of Engineering & Technology.I am passionate about all types of programming.

Published by

Syed Adeel Ahmed

Computer Systems Engineer from Sir Syed University Of Engineering & Technology.I am passionate about all types of programming.