(Scope Resolution Operator) What’s the purpose of the scope resolution operator?
To write the purpose of the scope resolution operator.
Explanation of Solution
The scope resolution operator is mainly used in C++ and it is denoted by double colon (: :).
Suppose in a program, there are two variables one local variable and another global variable with the same name, now if there is a need to access the global variables, scope resolution operator (: :) operator will be used. Thus it is used in order to differentiate the member functions of the class and the normal functions of the class.
The main purpose of the scope resolution operator is to identify and specify the context of an identifier. It defines the already declared member in .h extension or .cpp header files of the particular class.
Example program to describe the purpose of Scope Resolution Operator (: :)
#include<iostream> usingnamespacestd; /*initialize a global variable*/ int a=10; intmain() { /*initialize the local variable with the same name*/ int a=15; /*print the global variable using scope resolution operator*/ cout<<"The global variable is: "<<::a<<endl; /*print the local variable*/ cout<<"The local variable is: "<<a<<endl; return0; }
Output:
The global variable is: 10
The local variable is: 15
Want to see more full solutions like this?
Chapter 17 Solutions
C How to Program (8th Edition)
Additional Engineering Textbook Solutions
Starting Out with Java: From Control Structures through Objects (6th Edition)
Differential Equations: Computing and Modeling (5th Edition), Edwards, Penney & Calvis
Introduction to Programming Using Visual Basic (10th Edition)
Web Development and Design Foundations with HTML5 (9th Edition) (What's New in Computer Science)
Absolute Java (6th Edition)