0% found this document useful (0 votes)
31 views5 pages

Definition of Scope Operator

The scope resolution operator (::) is used in C++ to differentiate between member functions of a class and normal functions. It is placed between the class name and member function name (e.g. ship::foo()) to call a member function foo() from class ship. The scope resolution operator is also used to resolve the scope of variables when the same identifier is used for global, local, and class member variables. It references the global variable if placed before the variable name, or the class data member if placed between the class name and data member.

Uploaded by

manjulam.csi
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PPT, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
31 views5 pages

Definition of Scope Operator

The scope resolution operator (::) is used in C++ to differentiate between member functions of a class and normal functions. It is placed between the class name and member function name (e.g. ship::foo()) to call a member function foo() from class ship. The scope resolution operator is also used to resolve the scope of variables when the same identifier is used for global, local, and class member variables. It references the global variable if placed before the variable name, or the class data member if placed between the class name and data member.

Uploaded by

manjulam.csi
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PPT, PDF, TXT or read online on Scribd
You are on page 1/ 5

Definition of Scope Operator

&
Its Use
Scope Operator
The scope resolution operator (::) in C++ is used to define the
already declared member functions (in the header file with
the .hpp or the .h extension) of a particular class. In the .cpp file
one can define the usual global functions or the member
functions of the class. To differentiate between the normal
functions and the member functions of the class, one needs to
use the scope resolution operator (::) in between the class name
and the member function name i.e. ship::foo() where ship is a
class and foo() is a member function of the class ship. The other
uses of the resolution operator is to resolve the scope of a
variable when the same identifier is used to represent a global
variable, a local variable, and members of one or more class(es).
If the resolution operator is placed between the class name and
the data member belonging to the class then the data name
belonging to the particular class is referenced. If the resolution
operator is placed in front of the variable name then the global
variable is referenced. When no resolution operator is placed
then the local variable is referenced.
#include<iostream.h>
#include < conio.h >
Class bca
{
Private :
int a
int b;
Public:
Void get();
Void dis(); // member Function/Interface operations
}; // end of class definition

Void bca :: get()


{
// coding part
}
Void bca::dis()
{
// coding part
}
void main()
{
Clrscr()’

Class bca b;

b.get();
b.dis();
getch();
There are two used of the scope resolution
operator in C++.

 The first use being that a scope


resolution operator is used to unhide the
global variable that might have got hidden
by the local variables. Hence in order to
access the hidden global variable one
needs to prefix the variable name with the
scope resolution operator (::). e.g. int i =
10; int main () { int i = 20; Cout << i; // this
prints the value 20 Cout << ::i; // in order
to use the global i one needs to prefix it
with the scope //resolution operator. }
use of Scope Operator

 The second use of the operator is used to access


the members declared in class scope. Whenever a
scope resolution operator is used the name of the
member that follows the operator is looked up in
the scope of the class with the name that appears
before the operator.`

You might also like