Scope Resolution Operator in C++This section will discuss the scope resolution operator and its various uses in the C++ programming language. The scope resolution operator is used to reference the global variable or member function that is out of scope. Therefore, we use the scope resolution operator to access the hidden variable or function of a program. The operator is represented as the double colon (::) symbol. For example, when the global and local variable or function has the same name in a program, and when we call the variable, by default it only accesses the inner or local variable without calling the global variable. In this way, it hides the global variable or function. To overcome this situation, we use the scope resolution operator to fetch a program's hidden variable or function. Uses of the scope resolution Operator
Program to access the hidden value using the scope resolution (::) operatorProgram1.cpp Output The value of the local variable num: 100 The value of the global variable num: 50 Program to define the member function outside of the class using the scope resolution (::) operatorProgram2.cpp Output It is the member function of the class. Program to demonstrate the standard namespace using the scope resolution (::) operatorProgram3.cpp Output Enter the value of num: 50 The value of num is: 50 Program to access the static variable using the scope resolution (::) operatorProgram4.cpp Output The value of the static integer n1: 5 The value of the local variable n1: 15 The value of the Base::n2 = 10 Program to access the static member function using the scope resolution (::) operatorProgram5.cpp Output Use scope resolution operator to access the static member. Program to override the member function using the scope resolution (::) operatorProgram5.cpp Output It is the test() function of the ABC class. It is the test() function of the child class. Next TopicStatic Member Function in C++ |
We provides tutorials and interview questions of all technology like java tutorial, android, java frameworks
G-13, 2nd Floor, Sec-3, Noida, UP, 201301, India