Showing posts with label Scope Resolution. Show all posts
Showing posts with label Scope Resolution. Show all posts

Scope Resolution Operator in C++

In C,Suppose there are two variables with the same name 'c'.Assume that one is declared outside the function(global) and another is declared locally inside the function.
If we attempt to access variable 'c' inside the function we always access the local variable.Basic rule says that whenever there is conflict between local variable and global variable,the local variable gets the priority.

C++ allows you flexibility of accessing both the variables using scope resolution operator(::).

Example:
             #include<iostream.h>
             int x=10;
             void main()
             {
                       int x=20;
                       cout<<"First"<<x<<::x;
                       {
                             int x=30;
                             cout<<"Second"<<x<<::x;
                       }
                       cout<<"Third"<<x<<::x;
             }
             
             OutPut:
                         20  10
                         30  10
                         20  10