Code360 powered by Coding Ninjas X Naukri.com. Code360 powered by Coding Ninjas X Naukri.com
Table of contents
1.
Introduction
2.
Accessing a global variable when there is a local variable with the same name
2.1.
C++
3.
Defining a function outside a class
3.1.
C++
4.
Accessing a class's static variables
4.1.
C++
5.
Using scope resolution operator in multiple inheritance:
5.1.
C++
6.
Using scope resolution operator for namespace
6.1.
C++
7.
Referring to a class inside another class
7.1.
C++
8.
Referring to a member of the base class in the derived object
8.1.
C++
9.
Frequently Asked Questions
9.1.
Can the scope resolution operator be used with variables?
9.2.
Is it necessary to use the scope resolution operator to define member functions outside the class?
9.3.
Can the scope resolution operator be used to access private members of a class?
10.
Conclusion
Last Updated: Jul 15, 2024
Easy

Scope Resolution Operator in C++

Author Riya Singh
0 upvote

Introduction

In C++, the scope resolution operator (::) is a special symbol used to access certain elements in the code. It helps programmers specify which variable, function, or class they are referring to, especially when there are multiple items with the same name. The scope resolution operator is an important tool for organizing and managing code in larger projects. 

Scope Resolution Operator in CPP

In this article, we will discuss about the various uses of the scope resolution operator with examples to show how it can be used.

Accessing a global variable when there is a local variable with the same name

In C++, it's possible to have a local variable with the same name as a global variable. When this happens, the local variable takes precedence within its scope. However, if you need to access the global variable instead, you can use the scope resolution operator (::).

For example:

  • C++

C++

#include <iostream>

int x = 10;  // Global variable

int main() {

   int x = 20;  // Local variable with the same name as the global variable  

   std::cout << "Local x: " << x << std::endl;

   std::cout << "Global x: " << ::x << std::endl;   

   return 0;

}
You can also try this code with Online C++ Compiler
Run Code


In this code, we have a global variable `x` with a value of 10 and a local variable `x` inside the `main()` function with a value of 20. By using `::x`, we can access the global variable `x` even though there is a local variable with the same name.

Output:

Local x: 20
Global x: 10


As you can see, the scope resolution operator allows us to differentiate between the local and global variables with the same name.

Defining a function outside a class

In C++, you can define a function inside a class, making it a member function. However, you can also define a function outside the class using the scope resolution operator (::). This is useful when you want to separate the function declaration and definition or when you have a large function body that you want to keep outside the class for better readability.

For example:

  • C++

C++

#include <iostream>

class MyClass {

public:

   void myFunction();

};

void MyClass::myFunction() {

   std::cout << "This function is defined outside the class." << std::endl;

}

int main() {

   MyClass obj;

   obj.myFunction();

   return 0;

}
You can also try this code with Online C++ Compiler
Run Code


In this code, we have a class named `MyClass` with a member function `myFunction()`. The function is declared inside the class, but its definition is outside the class using the scope resolution operator `MyClass::myFunction()`.

When we create an object of `MyClass` and call the `myFunction()`, it will execute the function defined outside the class.

Output:

This function is defined outside the class.


By using the scope resolution operator, we can define member functions outside the class, providing flexibility in organizing our code.

Accessing a class's static variables

In C++, static variables are variables that belong to the class itself rather than to individual objects of the class. They are shared among all objects of the class. To access a static variable, use the scope resolution operator (::) along with the class name.
For example:

  • C++

C++

#include <iostream>

class MyClass {

public:

   static int myStaticVariable;

};

int MyClass::myStaticVariable = 10;

int main() {

   std::cout << "Static variable value: " << MyClass::myStaticVariable << std::endl;   

   MyClass::myStaticVariable = 20;  

   std::cout << "Modified static variable value: " << MyClass::myStaticVariable << std::endl; 

   return 0;

}
You can also try this code with Online C++ Compiler
Run Code


In this code, we have a class named `MyClass` with a static variable `myStaticVariable`. The static variable is declared inside the class, but its definition is outside the class using the scope resolution operator `MyClass::myStaticVariable`.

We can access the static variable directly using the class name and scope resolution operator, without creating any objects of the class. In the `main()` function, we print the value of `myStaticVariable`, modify it, and then print the modified value.

Output:

Static variable value: 10
Modified static variable value: 20


As you can see, the static variable is accessible and modifiable using the scope resolution operator and the class name.

Using scope resolution operator in multiple inheritance:

In C++, multiple inheritance allows a class to inherit from more than one base class. When a derived class inherits from multiple base classes, there may be ambiguity if the base classes have members with the same name. To resolve this ambiguity and specify which base class's member you want to access, you can use the scope resolution operator (::).

For example:

  • C++

C++

#include <iostream>

class BaseClass1 {

public:

   void myFunction() {

       std::cout << "BaseClass1::myFunction()" << std::endl;

   }

};


class BaseClass2 {

public:

   void myFunction() {

       std::cout << "BaseClass2::myFunction()" << std::endl;

   }

};


class DerivedClass : public BaseClass1, public BaseClass2 {

public:

   void callBaseFunctions() {

       BaseClass1::myFunction();

       BaseClass2::myFunction();

   }

};


int main() {

   DerivedClass obj;

   obj.callBaseFunctions();

   return 0;

}
You can also try this code with Online C++ Compiler
Run Code


In this code, we have two base classes `BaseClass1` and `BaseClass2`, both with a member function named `myFunction()`. The derived class `DerivedClass` inherits from both base classes.

Inside the `callBaseFunctions()` member function of `DerivedClass`, we use the scope resolution operator to specify which base class's `myFunction()` we want to call. `BaseClass1::myFunction()` calls the `myFunction()` from `BaseClass1`, and `BaseClass2::myFunction()` calls the `myFunction()` from `BaseClass2`.

Output

BaseClass1::myFunction()
BaseClass2::myFunction()


By using the scope resolution operator, we can resolve the ambiguity and explicitly specify which base class's member function we want to invoke.

Using scope resolution operator for namespace

In C++, namespaces are used to group related code elements and avoid naming conflicts. When you have multiple namespaces or nested namespaces, you can use the scope resolution operator (::) to access the elements within those namespaces.

For example:

  • C++

C++

#include <iostream>

namespace MyNamespace {

   void myFunction() {

       std::cout << "MyNamespace::myFunction()" << std::endl;

   }

}

namespace OuterNamespace {

   namespace InnerNamespace {

       void myFunction() {

           std::cout << "OuterNamespace::InnerNamespace::myFunction()" << std::endl;

       }

   }

}

int main() {

   MyNamespace::myFunction();

   OuterNamespace::InnerNamespace::myFunction();

   return 0;

}
You can also try this code with Online C++ Compiler
Run Code


In this code, we have two namespaces: `MyNamespace` and `OuterNamespace`. Inside `OuterNamespace`, we have another namespace called `InnerNamespace`. Each namespace has a function named `myFunction()`.

To access the `myFunction()` within `MyNamespace`, we use `MyNamespace::myFunction()`. To access the `myFunction()` within the nested namespace `OuterNamespace::InnerNamespace`, we use `OuterNamespace::InnerNamespace::myFunction()`.

Output

MyNamespace::myFunction()
OuterNamespace::InnerNamespace::myFunction()

By using the scope resolution operator `::`, we can specify which namespace's function we want to call, even if the namespaces are nested.

Referring to a class inside another class

In C++, you can refer to a class inside another class using the scope resolution operator (::). This is useful when you want to use a class as a member variable or a return type of a function in another class.

For example:

  • C++

C++

#include <iostream>

class InnerClass {

public:

   void innerFunction() {

       std::cout << "InnerClass::innerFunction()" << std::endl;

   }

};

class OuterClass {

public:

   InnerClass innerObject;

  

   void outerFunction() {

       innerObject.innerFunction();

   }   

   InnerClass getInnerObject() {

       return innerObject;

   }

};

int main() {

   OuterClass outerObject;

   outerObject.outerFunction();  

   OuterClass::InnerClass obj = outerObject.getInnerObject();

   obj.innerFunction();   

   return 0;

}
You can also try this code with Online C++ Compiler
Run Code

This code has two classes: `InnerClass` and `OuterClass`. Inside `OuterClass`, we have a member variable `innerObject` of type `InnerClass`. We also have a member function `outerFunction()` that calls the `innerFunction()` of the `innerObject`.

In the `main()` function, we create an object of `OuterClass` called `outerObject`. We call the `outerFunction()` using `outerObject`, which in turn calls the `innerFunction()` of the `innerObject`.

We also have a function `getInnerObject()` in `OuterClass` that returns an object of `InnerClass`. In the `main()` function, we use the scope resolution operator to specify the return type of `getInnerObject()` as `OuterClass::InnerClass`. This allows us to create an object `obj` of `InnerClass` and call its `innerFunction()`.

Output

InnerClass::innerFunction()
InnerClass::innerFunction()


Using the scope resolution operator `::`, we can refer to a class inside another class, either as a member variable, return type, or parameter type.

Referring to a member of the base class in the derived object

In C++, when a derived class inherits from a base class, it can access the public and protected members of the base class. However, if the derived class has a member with the same name as a member in the base class, it can lead to ambiguity. To resolve this ambiguity and explicitly refer to the base class member, you can use the scope resolution operator (::).

For example:

  • C++

C++

#include <iostream>

class BaseClass {

public:

   void myFunction() {

       std::cout << "BaseClass::myFunction()" << std::endl;

   }

};


class DerivedClass : public BaseClass {

public:

   void myFunction() {

       std::cout << "DerivedClass::myFunction()" << std::endl;

       BaseClass::myFunction();  // Calling base class function

   }

};

int main() {

   DerivedClass obj;

   obj.myFunction();

   obj.BaseClass::myFunction();  // Calling base class function using scope resolution  

   return 0;

}
You can also try this code with Online C++ Compiler
Run Code


In this code, we have a base class `BaseClass` with a member function `myFunction()`. The derived class `DerivedClass` inherits from `BaseClass` and also has a member function with the same name `myFunction()`.

Inside the `myFunction()` of `DerivedClass`, we call the `myFunction()` of the base class using `BaseClass::myFunction()`. This explicitly specifies that we want to call the base class function.

In the `main()` function, we create an object `obj` of `DerivedClass`. When we call `obj.myFunction()`, it invokes the `myFunction()` of `DerivedClass`, which in turn calls the `myFunction()` of `BaseClass`.

We can also directly call the base class function using the scope resolution operator on the derived class object, like `obj.BaseClass::myFunction()`.

Output

DerivedClass::myFunction()
BaseClass::myFunction()
BaseClass::myFunction()

Frequently Asked Questions

Can the scope resolution operator be used with variables?

Yes, the scope resolution operator can be used with variables to access global variables when there are local variables with the same name.

Is it necessary to use the scope resolution operator to define member functions outside the class?

No, it's not necessary, but using the scope resolution operator allows you to separate the function declaration and definition, improving code readability.

Can the scope resolution operator be used to access private members of a class?

No, the scope resolution operator does not grant access to private members of a class. Private members can only be accessed within the class or through public member functions.

Conclusion

In this article, we discussed the scope resolution operator (::) in C++ & its various uses. We learned how to access global variables, define functions outside a class, access static variables, resolve ambiguity in multiple inheritance, access elements within namespaces, refer to a class inside another class, & refer to base class members in derived objects. The scope resolution operator is a powerful tool that helps in organizing & managing code, especially in larger projects.

You can also practice coding questions commonly asked in interviews on Coding Ninjas Code360

Also, check out some of the Guided Paths on topics such as Data Structure and AlgorithmsCompetitive ProgrammingOperating SystemsComputer Networks, DBMSSystem Design, etc., as well as some Contests, Test Series, and Interview Experiences curated by top Industry Experts.

Live masterclass