Computer Science, asked by anshitsoni, 6 months ago

Develop programs using reference variable, scope

resolution operator, simple manipulator.​

Answers

Answered by sureshiyshsri
3

Answer:

In C++, scope resolution operator is ::. It is used for following purposes.

1) To access a global variable when there is a local variable with same name:

// C++ program to show that we can access a global variable

// using scope resolution operator :: when there is a local

// variable with same name

#include<iostream>

using namespace std;

int x; // Global x

int main()

{

int x = 10; // Local x

cout << "Value of global x is " << ::x;

cout << "\nValue of local x is " << x;

return 0;

}

Output:

Value of global x is 0

Value of local x is 10

2) To define a function outside a class.

// C++ program to show that scope resolution operator :: is used

// to define a function outside a class

#include<iostream>

using namespace std;

class A

{

public:

// Only declaration

void fun();

};

// Definition outside class using ::

void A::fun()

{

cout << "fun() called";

}

int main()

{

A a;

a.fun();

return 0;

}

Hope this help and please mark this as brainliest

Answered by s1249sumana10422
2

Scope refers to the visibility of variables. In other words, which parts of your program can see or use it. Normally, every variable has a global scope. Once defined, every part of your program can access a variable.

The scope resolution operator ( :: ) is used for several reasons. For example: If the global variable name is same as local variable name, the scope resolution operator will be used to call the global variable. It is also used to define a function outside the class and used to access the static variables of class.

Similar questions