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


31 Write a program to enter the name and salary for a number of employees, store them all in a file, and finally search and display the salary...............
Date: Sep 22, 2018
32 Write a program, using a class to read the contents of the text file and convert all upper case letters to lower case and vice-versa and write the result in to another file.
Date: Sep 22, 2018
33 Write a program to enter the id and price of a number of products, store them all in a user defined file, and search the product when particular id is entered.
Date: Sep 22, 2018
34 Write a program using class template to sort two lists of numbers, one integer list and another floating point list.
Date: Sep 23, 2018
35 Write a program using function template to sort two lists of numbers, one integer list and another floating point list.
Date: Sep 23, 2018
36 Write a program using function template to find the sum of first and last element of an array.
Date: Sep 23, 2018
37 Write a template function to find the maximum number from the template array of size N.
Date: Sep 23, 2018
38 Write a function template to swap the numbers.
Date: Sep 23, 2018
39 Write a program using class template to swap two set of numbers.(one integer set and another floating point set)
Date: Sep 23, 2018
40 Exception handling in C++
Date: Aug 11, 2019
41 Explain the concept of encapsulation and information hiding In an object oriented paradigm with the help of an example.
Date: Sep 2, 2019
42 Write a program to find sum of first and last element of an array demonstrating use of new and delete operator.
Date: Sep 2, 2019
43 Write a program to compare two integer numbers and two single character using function overloading.
Date: Sep 2, 2019
44 Write an oop to create a class named “Time” with data objects and two member functions to add two member functions to add two times and display the resultant time.
Date: Sep 2, 2019
45 Write a program to define a class “string” using constructor initialize two string objects with given strings and write necessary functions to concatenate and display the strings.
Date: Sep 2, 2019

Tools/Apps





© Nepal Exchange Rates