STATIC DATA MEMBERS AND STATIC FUNCTIONS MADE BY : MOHIT AGARWAL 161080107026 COMPUTER SEM - 4
CONTENTS Static Data Member Static Member Function Example - Program
STATIC DATA MEMBERS We can define class members static using static keyword. A static member variable has certain special characteristics: S hared by all objects of the class. I nitialized to zero when the first object is created. I nitialized outside the class .
A B A B OBJECT WITH TWO NORMAL DATA MEMBERS OBJECT WITH ONE NORMAL AND ONE STATIC DATA MEMBER
STATIC MEMBER FUNCTIONS A static function can access only other static members. It is called using class name and scope resolution operator. class name :: function name; It can not use this pointer.
EXAMPLE #include < iostream.h > #include < conio.h > class test { int code; static int count; //Static Member public: void set(void) { code=++count; } void show(void) { cout <<"Object number:"<<code<< endl ; } static void showcount (void) //Static Function { cout <<"Count:"<<count<< endl ; } };
int test::count; //Initializing Static Member void main() { test t1,t2,t3; clrscr (); t1.set(); test:: showcount (); t2.set(); test:: showcount (); //Accessing Static Function t3.set(); test:: showcount (); t1.show(); t2.show(); t3.show(); getch (); }