Concept of Static Data in Object Oriented Programming.

If a data item in a class is defined as STATIC then one such item is created for the entire class no matter how many object created . A static data item is useful when all object of same class share a common information.

Example:

// Cpp program to illustrate the 
// concept of static data 
#include <iostream> 
using namespace std; 
  
class counter{
    public: 
    static int count;
    counter()     
    {
        count++;
    }
    
    int display()
    {
       return count;
    }
};
int counter::count=10;
int main(){
    counter c1,c2,c3;
    cout<<"value of count when object1 created "<<c1.display()<<"\n";
    cout<<"value of count when object3 created "<<c3.display()<<"\n";
    cout<<"value of count when object2 created "<<c2.display()<<"\n";
    return 0;
}

Output:




Comments

Popular posts from this blog

Introduction to Python.

Decision Making Statement.