Page 52 - DCAP404 _Object Oriented Programming
P. 52

Unit 2: Beginning of OOP Language




          11.  The ……………… statement causes the program flow to exit the body of the while loop.  Notes
          12.  The do-while loop displays the ……………………. value of digit, increases its value by 1.

          2.5 Scope Resolution Operator

          C++ is also a block-structured language. We know that the same variable name can be used to
          have different meanings in different blocks. The scope of a 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. Blocks in C++ are often nested.  For example the following
          style is common:
          . . . . .

          . . . . .
          {
                 int  x=10;
                 . . . . .
                 . . . . .
          {
                     int x=1;

                    . . . . .
                    . . . . .
          }
          . . .
          .  .
          }
          The declaration in an inner block hides a declaration of the same variable in the outer block. C++
          resolves this problem by introducing a new operator ::  called the scope resolution operator.
          This can be used to uncover a hidden variable. It takes the following format:

          ::variable_name
          This operator allows access to the global version of a variable.
          Program illustrating the use of :: is given below:
          #  include  <iostream.n>
          int  m=10;            //globalm
          main()
          {

          int  m=20             //m  is  redeclared;  local  to  main
                 {
              int  n=m;
              int  m=30;  //m  is  declared  again
              cout  <<  “we  are  in  innerblock  \n”;





                                           LOVELY PROFESSIONAL UNIVERSITY                                   45
   47   48   49   50   51   52   53   54   55   56   57