Static Data , Static Functions

Sep 2, 2018

Static Data :

In C++, memory is allocated for the object when it is created(class is instantiated). That means the object comes into live when it is created and the memory is allocated for all the members of the object. And the every object has its own memory allocations. But,for the data members declared as static doesn't behave like that.

For static data members only one space is allocated for the entire class no matter how many objects there are. A static data item is useful when all objects of the same class must share a common item of information. A static variable defined as static has characteristic similar to a normal static variable. Static data member do not belong to a concrete object but to a class in general. So, static data members are also called class variable. It is visible only within the class, but its lifetime is the entire program, but are limited to the scope of the class whether they are private, public or static. It continues to exist even if there are no item of the class. As static data belongs to a class but not to the objects of that class, it can be accessed before the creation of any objects of that class.

Unlike non static member data, static data members require two seperate statements in different location for declaration and dfination. The static data members is like general static variables, they are declared using static keyword. The general syntax for declaring the static members in class is:

class CLASS_NAME{
//.................
SPECIFIER:
static DATA_TYPE data_member;//declaration
//.................
};
DATA_TYPE CLASS_NAME::data_member = INITIAL_VALUE;//declaration, initialization is optional

 Example: Let's consider that we have a class Ball in pool game, now to hold the number of ball in the pool we can use static variable for the class Ball.

#include<iostream>
using namespace std;

class Ball{
    private:
        static int ball_in_board;
    public:
        Ball(){
            ball_in_board++;
        }
        int getBallNumber(){
            return ball_in_board;
        }
        ~Ball(){
            ball_in_board--;
        }
};
int Ball::ball_in_board = 0;

int main(){
    short int ball_dead_status1 = 1;
    Ball *solid_stripe_ball1 = new Ball;
    cout<<"Numberof solid stripe ball:"<getBallNumber()<<endl;
    Ball *spot_stripe_ball1 = new Ball;
    cout<<"After spot stripe ball Added no:"<getBallNumber()<<endl;
    if(ball_dead_status1){
        delete spot_stripe_ball1;
    }
    cout<<"After spot stripe Dead ball no:"<getBallNumber()<<endl;
    return 0;
}

 

If the private members of the class are static they can not be accessed directly using class name as well as object. i.e. 

CLASS_NAME::private_member;  it is illegal to access

object.private_member;  it is illegal to access

the above situation is due to the access modifier of the member.

Static Function :

 Since, the private static datas can't be accessed before object creation directly using class and normal member functions can not be invoked before the object creation; the function with static type came into existance to overcome the this problem. Like static data static function does not belong to a object but to a class. The static functions can be called without the object creation through the class using scope resolution operator as.

CLASS_NAME::STATIC_FUNCTION();

the general syntax declaring and defining the static function is 

class CLASS_NAME

{

    //..............................

   ACCESS_MODIFIER:

      static RETURN_TYPE FUNCTION_NAME(){

         //.........................

        //function body

      }

   //...............................

};

 

 

Related


Tools/Apps





© Nepal Exchange Rates