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


16 Default arguments in C++
Date: Sep 2, 2018
17 Friend Function, Friend class
Date: Sep 4, 2018
18 What are friend functions ? Is it possible for a function to be friend of two classes ? If yes then how is it implemented in C++. Explain with suitable example. ---PU-2016 (New Course)
Date: Sep 10, 2018
19 Write an OOP to add two complex numbers using passing objects as arguments. ---PU_2015
Date: Sep 10, 2018
20 Write a Program to add two complex numbers using friend function. ----PU_2015
Date: Sep 10, 2018
21 What are constructors and destructors ? Write a Object Oriented Program to display Fibonacci series (Use constructor for initialization) ----PU-2014
Date: Sep 15, 2018
22 Write a OOP to create a class named "Time" with data members 'hour' and 'minute' a constructor to initialize............. ---PU-2011
Date: Sep 15, 2018
23 What is "this" pointer ? Illustrate with example.
Date: Sep 15, 2018
24 Write a program to create class “Counter” having integer data member, overload the post increment, post decrement, pre increment and pre decrement for the class “Counter”.
Date: Sep 15, 2018
25 Write a Program to overload the unary minus operator using friend function.
Date: Sep 15, 2018
26 Write a OOP to overload "==" operator to determine the equality of two fractional numbers (fractional numbers must be in terms of numerator and denominator)
Date: Sep 15, 2018
27 Write a program to create classes “Dollar” and “Rupee”. Write conversion routine to convert dollar to rupee (1 dollar = 108 rupees).
Date: Sep 15, 2018
28 Write a Program to convert angle in degree to angle in radian. where, angle in radian = angle in degree * PI/180. Use Conversion routine in source class.
Date: Sep 15, 2018
29 Write a program which asks file name from keyboard, opens a file with that name for output, reads the line from keyboard character by character and writes the line onto the file.
Date: Sep 22, 2018
30 WAP to to read the text from the file named "hello.txt" to the file name called "test.txt".
Date: Sep 22, 2018

Tools/Apps





© Nepal Exchange Rates