Scope Resolution Operator In C++


A class can have any number of member functions and each of the member functions can have any number of instructions. A class definition should be in such a way that at a glance one should be able to know the members of a class. So we define the member functions outside the class definition. Hence make it a habit. From this point onwards we define the member functions outside the class only.
There can be any number of classes in a program and functions with the same signature can occur in different classes. So,:: operator resolves the scope of a function is called Scope resolution operator. Syntax for defining a member function
return-value type <class name> :: <member function name> { body of the member function; }

Example of Scope Resolution Operator in C++

#include<iostream> using namespace std; class MyFirst{ void country1(); public: void country2(); void country3(); }; void MyFirst :: country1(){ cout<<"Welcome to country1"<<endl; } void MyFirst :: country2(){ country1(); cout<<"Welcome to country2"<<endl; } void MyFirst :: country3(){ cout<<"Welcoem to country3"<<endl; } int main(){ MyFirst f; f.country2(); f.country3(); return 0; }

Output:-

Welcome to country1
Welcome to country2
Welcome to country3

Explanation of the program

Here, the:: operator is used to indicating that the functions country1(), country2(), country()3 belong to the class MyFirst.