What is the use of scope resolution operator in C++
What is the use of scope resolution operator in C++
- define the data memberĀ outside of the class
- define the member function outside of the class
- demonstrate the standard namespace
- access the static variable
c++ scope resolution operator examples
Scope resolution operator (::) is used to define the data memberĀ outside of the class
1 2 3 4 5 6 7 8 9 10 11 12 |
#include <iostream> using namespace std; // declare global variable int num = 50; int main () { int num = 100; cout << " The value of the local variable num: " << num; // use scope resolution operator (::) to access the global variable cout << "\n The value of the global variable num: " << ::num; return 0; } |
Scope resolution operator (::) is used to define the member function outside of the class
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 |
#include <iostream> using namespace std; class T4Tutorials { public: // declaration of the member function void Function(); }; // define the member function outside the class. void T4Tutorials::Function() /* return_type Class_Name::function_name */ { cout << " It is the member function of the class. "; } int main () { // create an object of the class T4Tutorials T4Tutorials Object; Object.Function(); return 0; } |
Scope resolution operator (::) is used to demonstrate the standard namespace
1 2 3 4 5 6 7 8 9 |
#include <iostream> int main () { int Number = 4; // use scope resolution operator with std namespace std :: cout << " Please Enter the value of Number: "
; std::cin >> Number; std:: cout << " The value of Number is: " << Number; } |
Scope resolution operator (::) is used to access the static variable
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 |
#include <iostream> using namespace std; class BaseClass { static int Number1; public: static int Number2; // The class member can be accessed using the scope resolution operator. void T4Tutorials ( int Number1) { // Number1 is accessed by the scope resolution operator (:: ) cout << " The value of the static integer Number1: " << BaseClass::Number1; cout << " \n The value of the local variable Number1: " << Number1; } }; // define a static member explicitly using :: operator int BaseClass::Number1 = 5; // declare the value of the variable Number1 int BaseClass::Number2 = 10; int main () { BaseClass object; int Number1 = 15; object.T4Tutorials (Number1); cout << " \n The value of the Base::Number2 = " << BaseClass::Number2; return 0; } |
Frequently Asked Questions about scope resolution operator
- In c++, the scope resolution operator is ____.
- In c++, ____ is called the scope resolution operator.
-
The symbol represents the c++ās scope resolution operator.
-
What characters are used for the scope resolution operator?
-
What is the purpose of the scope resolution operator?
-
What are the advantages of scope resolution operator usage in C++?