Asif Mushtaq
Asif Mushtaq

Reputation: 3798

What is Object Scope in Oop?

Hi I read this definition on internet..

A destructor is a special member function of a class that is executed whenever an object of it's class goes out of scope or whenever the delete expression is applied to a pointer to the object of that class.

I tried this code..

#include <iostream>
using namespace std;
class Name{
    public:
        void Fun(){
            cout << "Function" << endl;
        }
        Name(){
            cout << "Constructor" << endl;
        }
        ~Name(){
            cout << "Destructor" << endl;
        }
};
int main(){
    Name obj;
    obj.Fun();
    cout << "End" << endl;
    return 0;
}

It's calling destructor at the end of program..!! Mean object scope is the end of program? Sorry I'm little confused...

Upvotes: 1

Views: 4874

Answers (4)

Vaibhav Sharma
Vaibhav Sharma

Reputation: 1

In your case its C# program, In the case of object ends it availability after the program ends i.e. after the main is executed the object finds its destructor if it’s not available by programmer then its system generated destructor will be executed an it will destroy all the object and frees up the memory..

Upvotes: 0

Daniyar
Daniyar

Reputation: 1

The object obj that you have created is a variable and as any variable it has scope. Scope is defined by curly brackets in which variable was created. In your case the scope of obj that you created is main function, because it is placed between curly brackets of that function. And when main function ends, your object is not available anymore (it is available only within that function) and that is why destructor is called.

You can also define scope of your own by just putting your object into curly brackets like that:

main(){
    cout<<"scope of the main function"<<endl; 

    {
        cout <<"scope where obj created"<<endl;
        Name obj;
    }
    cout << "obj destroyed"<<endl;

    cout << "End of main" <<endl;
}

Upvotes: 0

Daniel Frey
Daniel Frey

Reputation: 56921

In your case the end of the scope of obj is at the end of the main function, but it could be any other scope depending on where you define obj. Example:

int main(){
    // open a new scope here
    {
        // now 'obj' will be in the new scope
        Name obj;
        obj.Fun();
    }
    // the scope ended here, 'obj' destroyed now
    cout << "End" << endl;
    return 0;
}

You can find more information here, look at "Basic concepts" for "Scope" and "Lifetime".

Upvotes: 3

luk32
luk32

Reputation: 16090

Scope is a region in program that defines life time of objects defined within it. In nearly all cases it is defined by curly brackets. So when you have definition of function it's body defines a scope.

main is no special in any way in regard to definition of scope.

Some more cases:

int fun(){ // one scope

  for(int i = 0; i < 1337; ++i){ // another scope
    //note that i is defined within `for` scope not outside
  }

  if(true){ // same here
  }

  { // you can even start and end scope at will
     A a;
  } // a will die here

  { // sometimes it is useful for redeclaring a variable, probably not best practice
     A a; // this is legal
  }
}

Upvotes: 2

Related Questions