Site icon T4Tutorials.com

What is the use of scope resolution operator in C++

What is the use of scope resolution operator in C++

  1. define the data member  outside of the class
  2. define the member function outside of the class
  3. demonstrate the standard namespace
  4. access the static variable

c++ scope resolution operator examples

Scope resolution operator (::) is used to define the data member  outside of the class

#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

#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

#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

#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

  1. In c++, the scope resolution operator is ____.
  2. In c++, ____ is called the scope resolution operator.
  3. The symbol represents the c++’s scope resolution operator.
  4. What characters are used for the scope resolution operator?
  5. What is the purpose of the scope resolution operator?
  6. What are the advantages of scope resolution operator usage in C++?
Exit mobile version