Friday, 21 February 2014

Static Data Member and Static Member Functions




STATIC DATA MEMBER

As we know Each object of a particular specified class consist of its own data . But if we declared Data item is as static data item then this static data item or we can simply say static variable or static member function will be available for global access for all objects of class . It means if you have declared a variable for member function as static then this can be accessed by all objects of class .

I mean to say if you have declared a variable as static then you have assigned it value '5'  through Obj1 object of class . Then if you have created a new object Obj2 and accessing value of static variable that it is still holding value '5' if it is accessed through Obj2 .


A Static Variable is visible or accessible only within the class or only by members of class But its lifetime is throughout the program means it can hold the same value throughout the lifetime of program


DECLARATION :-

                                  Static int stvar;

INITIALIZATION :- 

                
*                                     int abc : : stvar =10

        In this Stvar is initialized with value '10' and abc is class name . 

*              int abc : :  : stvar ;
In this abc is classname and now stvar will be assigned '0' value



SIMPLE PROGRAM TO SHOW USE OF STATIC VARIABLE


#include<iostream.h>
#include<conio.h>

class static_var
{

static int count;    //static member of class
public :


void incr_staticvar()
{
count++;
}

 void outputc()
{
cout<<"Value of Static variable Count :- "<<count<<endl;
}
};

int static_function : : count;

void main()
{
clrscr();
static_var obj1,obj2,obj3,obj4;

obj1.incr_staticvar();
obj2.incr_staticvar();
obj3.incr_staticvar();
obj4.incr_staticvar();

cout<<"\nAfter Increment of static variable by Four Different objects is :-\n";

obj1.outputc ( );
obj2.outputc ( );
obj3.outputc ( );
obj4.outputc ( );


      getch();
}

OUTPUT :-




STATIC MEMBER FUNCTION



The Word Static means fixed . To  make a member function static you have to precede the word static to it .

Facts About Static Member Functions :-

*  The main usage of static functions is they are made to access static members of class or static variables of class that are declared in same class in which static member functions are declared

* These can be called outside class or in main function without using Object of class.



HOW TO CALL STATIC MEMBER FUNCTION


CLASS_NAME  : :  NAME_OF_FUNCTION

SIMPLE PROGRAM TO SHOW USE OF STATIC MEMBER FUNCTIONS


#include< iostream.h >
#include< conio.h >

class static_function
{

int a;
static int count;    //Declaration of static Data member of class 

public :

static_function( )    // Constructor that is initializing variable a to 0
{
a=0;
}

void non_static( )    // public member function that is incrementing a variable
{
a++;
}

void outputn ( )  // public member function to cout value of a
{
cout<<"A :-  "<<a<<endl;
}

void incr_staticvar( )   // public member function to increment static variable
{
count++;
}

static void outputc( )  // public member function to cout value of static variable
{
cout<<"Value of Static variable Count :- "<<count<<endl;
}
};

int static_function :: count;

void main( )
{
clrscr();
static_function obj1,obj2,obj3,obj4;   //creation of four object of class static_function

cout<<"Initially ";
static_function : : outputc();     // calling static function

obj1.incr_staticvar();    //calling public funtion to increment static variable
obj2.incr_staticvar();
obj3.incr_staticvar();
obj4.incr_staticvar();



cout<<"\nAfter Increment of static variable by Four Different objects is :-\n";
static_function ::outputc();

obj1.non_static();    // function to increment a variable
obj2.non_static();
obj3.non_static();
obj4.non_static();

cout<<cout<<"After Increment of Variable 'A' by Four Different objects i :-";
obj1.outputn();
obj2.outputn();
obj3.outputn();
obj4.outputn();

      getch();

}

OUTPUT :-



Share this

4 Responses to "Static Data Member and Static Member Functions"

  1. Hurrah, that's what I was searching for, what a material!
    existing here at this web site, thanks admin of this website.


    my web page: roof windows Cape Town

    ReplyDelete
  2. Thanks for you great appreciation

    ReplyDelete
  3. really good One ...

    ReplyDelete
  4. The code is unreadable. Bad formatting. Please improve this otherwise nice material.

    ReplyDelete