Operators and Expressions in c++ - TGN

Latest

Welcome to Technicalgurunow! Your No. 1 source for all networking , earning and programming tutorials. We're dedicated to providing you the best of knowledgeable information with a focus on dependability. We hope you enjoy our blog.

Thursday, January 9, 2020

Operators and Expressions in c++

Operators in C++:-



in that post we learn about what is the Operator in c++?.

  • All C operators are valid in C++ also.
  • In addition, C++ introduces some new operators.
  • Other new operators are:-
           ::         Scope resolution operator
           ::*       Pointer to member declarator
           ->*      Pointer to member operator
           .*        Pointer to member operator
         delete    Memory release operator
         endl      Line feed operator
          new     memory allocation operator
          setw     field width operator
  • Various Operators and their parts are as follows:-

Scope Resolution Operator:-


  • Like C, C++ is also a block-structured language.
  • We know that the same variables name can be used to have different meanings in different blocks.
  • The scope of the variable extends from the point of its declaration till the end of the block containing the declaration.
  • A variable declared inside a block is said to be local to that block.
  • Example:


        {
              int i=10;

              {
                  int i=1;
               }
         }



  • Here, the inner block which contains int i=10, and the outer block which contains int i=1 are nested.
  • In C, the global scope of a variable cannot be accessed from within the inner block.
  • C++ resolves this problem by introducing the (::) operator called as Scope resolution operator.

  • Syntax:

     :: variable_name;
This operator allows access to the globle scope of a variable.


Member dereferencing operator:- 


  • A member dereference operator operates on a pointer variable.
  • It returns the location value i.e. l- value in memory pointed to by the variable's value.
  • Dereferencing operator is denoted as '* ' 
  • Syntax:-

        *pointer_variable

  • C++ provides a set of three pointer to member operators.




Operator 
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. 


Memory management operators:-

  • C uses malloc( ) and calloc( ) functions to allocate memory dynamically and the free( ) function to free dynamically allocated memory at runtime.
  • Similarly, C++ supports these functions. It defines unary operators new and delete, that perform the task of allocating and freeing the memory in a better way.
  • An object can be created by using new keyword and destroyed by using delete, as & when required.
  • An object created inside a block with new, will remain in existence throughout the program until it is explicitly destroyed by using delete.
  • The new operator can be used to create objects of any type.
  • Syntax:-

             pointer_variable= new data_type;
            example:-
               p=new int;
               q=new float;
  • The new operator can be used to initialize memory.
  • Syntax:-

        pointer_variable=new data_type(value);
          example:- Here, value specifies the initia value,
             int *p=new int(25);
             float *q= new float(7.5);
  • The new operator can be used to create a memory space for any data type including user defined types such as arrays, structures and classes.
  • Syntax:-

         pointer_variable= new data_type(size);
 Here, size specifies the number of elements in the array.
  • When we don't need a data object, it is destroyed to release the memory space for reuse.
  • Syntax:-

       delete pointer_variable;
        The pointer_variable is the pointer that points to a data object created with new.
         example:-
           delete p;
           delete q;
  • If we want to free a dynamically allocated array, then
  • Syntax:-

      delete[size] pointer_variable;
example:-  delete[ ] p;

Manipulators:-

  • Manipulators are the operators that are used to format the data display.
  • The most commonly used manipulators are endl and setw.
  • The endl manipulator, when used in an output statement, it terminate a line i.e. to end a line.
  • It produce a newline.
  • example:-

       //code
         cout<<"m="<<m<<endl
         cout<<"m="<<m<<endl
         cout<<"m="<<m<<endl;
      //code


  • The setw manipulator stands for set width.
  • It is used to specify the minimum number of character positions on the output a variable will consume.
  • example:-

       cout<<setw(5)<<sum<<endl;
Here, setw(5) specifies a field width 5 for printing the value of the variable sum.


Expressions and their types:-

  • An expression is a combination of operators, constants and variables arranged as per the rules of the language.
  • It may also contain function calls which return values.
  • It may consist of one or more operands, and zero or more operators to produce a value.
  • Following are the types of expressions:-

  1. Constant Expressions
  2. Integral Expressions
  3. Float Expressions
  4. Pointer Expressions
  5. Relational Expressions
  6. Logical Expressions
  7. Bitwise Expressions

  • Expressions can be the combination of the expressions mentioned above. These expressions are known as compound expressions.


1. Constant Expression:-

    Constant expressions consist of only constant values.

2.Integral Expressions:-

    Integral Expressions are the expressions which produces integer results after implementing all the automatic and explicit type conversions.

3.Float Expressions:-

   Float expressions are the expressions which produce floating point results after all conversions.

4.Pointer Expressions:-

   Pointer expressions produce address values.

5.Relational Expressions:-

   Relational expressions yield results of type bool which takes a value true or  false.Relational expressions are also known as Boolean expressions.

6.Logical Expressions:-

   Logical expressions combine two or more relational expressions and produces bool type results.

7.Bitwise Expressions:-

   Bitwise expressions are used to manipulate data at bit level. They are basically used for testing or shifting bits.

So here it is all about operators and expressions in C++.    

2 comments:

If you have any query, please let me know

Followers