Skip to main content

What is scope resolution operator in c++

Hello friends, in this post today, we will learn about What is scope resolution operator in c++ and how to use in our program. so let's start....

Scope resolution operator in c++,  Scope resolution operator
Scope resolution operator 




Scope resolution operator :-

Scope resolution operator which is also known as (membership lable), represnted as (::).


The scope resolution operator is employed to resolve or extend the scope of variable or functions.


C++ is block structured language. We know that the same variable or function name can be used to have different meaning in different block.


The scope resolution operator will refer value of worldwide variable or function from anywhere (also from inner block).


Without scope resolution operator all variable will refer local value.



Example for variabe:

#include <iostream> 
using namespace std;
int m=10;
int main()
{

     int m=20;
     {
     int k=m; 
     int m=30;
    cout<<"we are in inner block\n";
     cout<<"k="<<k<<"\n"
     cout<<"m="<<m<<"\n"
     cout<<"::m="<<::m<<"\n";
     }
         cout<<"we are in outer block\n";
          cout<<"m="<<m<<"\n"
          cout<<"::m="<<::m<<"\n";
           return 0;
}

Output:

we are in inner block
k=20
m=30
::m=10
we are in outer block
m=20
::m=10



Example for function:

#include<iostream>
using namespace std;

class scope
{
     public:
        void function();
};

void scope::function()
{
   cout<<"Demo of scope resolution operator! Thankyou";
}

int main()
{
  scope s;
  s.function();
  return 0;
}

Output:

Demo of scope resolution operator! Thankyou



I hope that whatever information I have given in this post today, you have liked it and you have understood it.so keep learning and wait for the next post that will help you to increase your knowledge with the something new information.

Thank you so much for reading. And take care about yourself and your family.


Comments

Popular posts from this blog

What is new and delete operator in c++

Hello friends, in this post today, we learn about What is new and delete operator in c++ . so lets start... new and delete operator  Memory Management Operators of C++:- The technique through which a program can obtain space in the RAM during the execution  of the program and not during compilation is called dynamic memory allocation. In this method space for the variables (array, strings, structure etc) is allocated from a free memory region which is known as heap.  For dynami c memory management, C++ provides two unary operator 'new' and 'delete'. Dynamic memory allocation is the allocation of memory at run time i.e., when program  executes then required memory is asked from the user and memory is allocated with the help of operator new. Syntax of new : data_type *ptr = new data_type; Here ptr is a variable of data_type. Memory is allocated by finding the size of data_type  automatically by the compiler and address of the allocated memory is returned which is ...

Destructor in c++

Hello friends, in this post today, we learn about what is Destructor in c++ and why we use Destructor in c++. so lets start... Destructor in c++ Destructor:- Destructor is a special and  instance member function of a class. Destructor is a member function whose name must be same as class name but is preceded by a tilde sign (~). The purpose of destructor is to destroy the object when it is no longer needed or goes out of scope. It should be defined to release resources allocated to an object. Features of Destructor:- Destructor should be declared in the public section. Destructor can never be static. Once a destructor is called for a object, the object will no longer be available for the  future reference. Destructor never  takes any argument nor it returns any value nor it has return type (no overloading is possible). Destructor is invoked automatically by the complier when object goes out scope  of the program. There is no explicit or implicit category for a dest...

'this' pointer in c++

Hello friends, i n this post today, we learn about 'this' pointer in c++ and what is the  use  of   'this'  pointer .  so lets start... this pointer in c++ 'this' pointer:- The this pointer is a special pointer which is a built-in pointer. and  'this' is a keyword. It stores the  address of current object in context. That is the current object which can be referred using this  pointer anywhere in the class. Important Points About this Pointer:- The this pointer can be used only inside the class i.e., only  inside the member function of the class and cannot be used outside and  static member of the class. The object’s address is available from within the member function as the this pointer. It  is legal, through unnecessary, to use this pointer when referring to members of the class. The  expression (*this) is commonly used to return the current object from a member function. this is a local object pointer in every instanc...