AppDividend
Latest Code Tutorials

Scope Resolution Operator in C++ Example Tutorial

0

Scope Resolution Operator in C++ Example Tutorial is today’s topic. Scope resolution operator is used to access functions, variables which are not present in that particular scope but are existing in the program. In other words, the scope resolution operator is used to define the function outside a class; We can also use it to set a global variable which has the same name as of a local variable in that particular scope.

#Scope Resolution Operator in C++

The :: (scope resolution) operator is used to get the hidden names due to the variable scopes so that you can still use them.

The scope resolution operator can be used as both as unary and binary.

You can use a unary scope operator if the namespace scope or global scope name is hidden by the particular declaration of an equivalent name during a block or class.

For example, if you have the global variable of name my_var and the local variable of name data_var, to access global data_var, you’ll need to use a scope resolution operator.

#Syntax of Scope Resolution Operator

See the following syntax.

::gobal_variable                        (In case of variables)

Ex- ::x=5; (x is the global variable)

In case of functions.

return_type  class_name::function_name

Ex- void rectangle::area(); (Here rectangle is the class name and area is the name of the function)

#Applications of Scope Resolution Operator

  1. If we want to define a function which is declared inside the class but not defined inside the class.
  2. We cannot directly access the static variable we use scope resolution operator to access static variables.
  3. If one variable with the same name exists in multiple classes(ancestors), we can access the variable with the help of the scope resolution operator.
  4. We can use the scope resolution operator to access the static variables of the class.

#Examples on scope resolution operator

Example 1: Write a program which declares the area function for a rectangle inside the class and defines it outside the class using scope resolution operator.

See the following program.

#include <iostream>
using namespace std;
class rectangle
{
public:
  void area(int, int); //declaring function
};
void rectangle::area(int length, int breadth) //defining function using scope resolution operator
{
  cout << "The area of the rectangle is: " << length * breadth << endl;
}
int main()
{
  rectangle obj; //creating object of rectangle class
  int x, y;
  cout << "Enter length:"; //taking inputs
  cin >> x;
  cout << "Enter breadth:";
  cin >> y;
  obj.area(x, y); //calling the function with desired length and breadth
  return (0);
}

See the following output.

 

Scope Resolution Operator

Example 2: Write a program to access static variables of a class using the scope resolution operator.

#include <iostream>
using namespace std;

class A
{
  static int static_variable;

public:
  void test(int static_variable)
  {

    cout << "Static variable in global scope=" << A::static_variable; //function printing the value of the variable in global and local scope

    cout << "\nStatic variable in local scope=" << static_variable;
  }
};

int A::static_variable = 5; //declaring variable in global scope

int main()
{
  A obj;
  int static_variable = 8;   //declaring variable in local scope
  obj.test(static_variable); //calling function with static variable as parameter
  return 0;
}

See the following output.

 

Scope Resolution Operator in C++

Example 3: Write a program to access a global variable which is already present in the local scope with the same name using scope resolution operator.

#include <iostream>
using namespace std;

int x = 2; // declaring the variable globally

int main()
{
  int x = 4; //declaring the variable locally
  int y = 5;
  cout << "Value of global x= " << ::x << endl;
  cout << "Value of local x= " << x << endl;
  cout << "Addition after using global variable= " << ::x + y << endl;
  cout << "Addition after using local variable= " << x + y << endl;
  return 0;
}

See the following output.

 

C++ Scope Resolution Operator

Example 4: Write a program to use a variable “length” to find the area of a rectangle as well as the area of the square by using the properties of multiple inheritances (Both square and rectangle member functions are present in two different inherited classes). Use scope resolution operator to access the variable length. 

See the following code.

#include <iostream>
using namespace std;

class A
{
public:
  int length, breadth;
  void getdata(int x, int y)
  {
    length = x;
    breadth = y;
  }

  void area()
  {
    cout << "Area of rectangle= " << length * breadth << endl;
  }
};

class B
{
public:
  int length;
  void getdata(int q)
  {
    length = q;
  }

  void area()
  {
    cout << "Area of square " << length * length << endl;
  }
};

class C : public A, public B
{
public:
  void callingfun()
  {
    A::getdata(5, 6);
    A::area();
    B::getdata(10);
    B::area();
  }
};

int main()
{
  C obj;
  obj.callingfun();
  return 0;
}

See the following output.

 

Applications of Scope Resolution Operator

Finally, the Scope Resolution Operator in C++ Example Tutorial is over.

Leave A Reply

Your email address will not be published.

This site uses Akismet to reduce spam. Learn how your comment data is processed.