Topics  Various operators     ׅArithmetic Operators      Relational Operators     Logical Operators     Assignment Operators     Inc...

Operators and Reference variable in C++

/
0 Comments
Topics 
Various operators
    ׅArithmetic Operators 
    Relational Operators
    Logical Operators
    Assignment Operators
    Increment and Decrement Operators 
    Conditional Operator 
    Special Operators 
    Extraction operator ( >>)
    Insertion operator (<<)
    Scope resolution operator (::) 
    member dereferencing operators

 Reference variable

Various operators used in C++. 

  • An operator is a symbol that tells the compiler to perform certain mathematical or logical operation. 

1. Arithmetic Operators 

  • Arithmetic operators are used for mathematical calculation. C++ support following arithmetic operators

 +Addition or unary plus
 -Subtraction or unary minus
 *Multiplication
 /Division
 % Modulo division


2. Relational Operators

  • Relational operators are used to comparing two numbers and taking decisions based on their relation.
  • Relational expressions are used in decision statements such as if, for, while, etc...


<     less than
<=   less than or equal to
>    greater than
>=  greater than or equal to
==  is equal to
!=   is not equal to 

3. Logical Operators

  • Logical operators are used to testing more than one condition and make decisions


 &&    logical AND (Both non zero then true, either is zero then false)
   ||       logical OR (Both zero then false, either is non zero then true)
   !       logical NOT (non zero then false, zero then true) 

4. Assignment Operators

  • Assignment operators are used to assigning the result Of an expression to a variable. C++ also supports shorthand assignment operators which simplify operation with the assignment.

 =         Assigns value Of right side to left side
 +=      a+=1 is same as a = a + 1
  -=      a-= 1 is same as a = a -1
 *=      a *=1 is same as a = a*1
 /=       a /=1 is same as a = a/ 1
 %=     a %= 1 is same as a = a % 1 

5. Increment and Decrement Operators 

  • These are special operators in C++ which are generally not found in other languages. 

+ +     Increments value by 1.
           a++ is postfix, the expression is evaluated first and then the value is                     incremented.
           Ex. a=10; b=a++; after this statement, 11, b = 10.
           ++a is prefix, the value is incremented first and then the expression is                      evaluated.
           Ex. a=10; b=++a; after this statement, 11, b = 11. 

 --    Decrements value by 1.
        a-- is postfix, the expression is evaluated first and then the value is                          decremented.
        Ex. a=10; b=a--; after this statement, 9, b = 10.
        --a is the prefix, the value is decremented first and then the expression is                     evaluated.
        Ex. a=10; b=--a; after this statement, 9, b = 9. 

6. Conditional Operator 
  • A ternary operator is known as Conditional Operator. 
  • expl?exp2:exp3 if expl is true then execute exp2 otherwise exp3 
Ex: x=(a>b)?a:b; which is same as 
if (a>b)
x=a; 
else 
x=b;


7. Special Operators 
 &     Address operator, it is used for determining address Of the variable.
*       Pointer operator, it is used to declare pointer variable and to get value                           from it.
,        Comma operator. It is used to link the related expressions together.
sizeof       It returns the number Of bytes the operand occupies.
.          member selection operator, used in the structure.
->       member selection operator, used in the pointer to structure. 

9. Extraction operator ( >>)

  • Extraction operator (>>) is used with cin to input data from the keyboard. 

10. Insertion operator (<<) 

  • Insertion operator (<<) is used with cout to output data from the keyboard. 

11. Scope resolution operator (::) 

  • Scope resolution operator (: : ) is used to define the already declared member functions Of the class. 
  •  The scope resolution operator is used to resolve or extend the scope Of variable. 
  •  C++ is block structured language. We know that the same variable name can be used to have a different meaning in the different block. 
  • The scope resolution operator will refer value Of global variable from anywhere (also from inner block). 
  • Without scope resolution operator all variable will refer local value. 

12. What is reference variable? 
• A reference variable provides an alias (alternate name) for a previously defined variable. 
• This mechanism is used in object-oriented programming because it permits the manipulation Of objects by reference, and eliminates the copying Of object parameters back and forth. 
• It is also important to note that references can be created not only for built-in data types but also for the user-defined data types.
• reference variable store the address of another variable .
• Syntax: Data_type & reference_name = variable_name 
• For example : 
int a=100; 
int &b=a;//Now both a and b will give the same value. 

8)  member dereferencing operators. 
  • C++ provides three-pointers to member operators to access the class member through the pointer. 
Operators        Function 
    ::*               TO declare a pointer to a member Of a class. 
    .*                TO access a member using object name and a pointer to that member. 
    ->*              TO access a member using a pointer to the object and a pointer to that member. 




You may also like

No comments:

Cybercry 2018. Powered by Blogger.

Popular Posts