Join the social network of Tech Nerds, increase skill rank, get work, manage projects...
 
  • Scope resolution operator

    • 0
    • 0
    • 0
    • 0
    • 0
    • 0
    • 0
    • 0
    • 511
    Comment on it

    In C++ scope resolution operator is used to define a function outside a class. If a user  has a local variable with same name as global variable and wants to use a global variable the we use the scope resolution operator.
     

    include<iostream.h>
    
    int m=10; //(global m)
    
    main()
    
    {
    
       int m=20; //m declared local to main
    
    {
    
      int K=m;
    
      int m=30;//m again local to inner block
    
      cout<<"we are in innnerbloack\n";
    
      cout<<"K="<<K<<"\n";
    
      cout<<"m="<<m<<"\n";
    
      cout<<"::m="<<::m<<"\n";
    
    }
    
    cout<<"\n we are in outerbloack \n";
    
    cout<<"m="<<m<<"\n";}
    
    cout<<"::m="<<::m<<"\n";

    Output is

     We are in inner block

         K=20;

        m=30;

      ::m=10;

    We are in outer block

        m=20;

      ::m=10;

    In the above program, variable m is declared mainly at following three places:

    (i) Outside the main() function

    (ii) Inside the main() function

    (iii) Inside the inner block.

    Note: ::m will always refer to the global m.In the inner block, ::m refer to value 10 and not 20.

     

     

 0 Comment(s)

Sign In
                           OR                           
                           OR                           
Register

Sign up using

                           OR                           
Forgot Password
Fill out the form below and instructions to reset your password will be emailed to you:
Reset Password
Fill out the form below and reset your password: