3. STATIC DATA MEMBERS
โข We can define class members static using static keyword.
โข A static member variable has certain special characteristics:
i. Shared by all objects of the class.
ii. Initialized to zero when the first object is created.
iii. Initialized outside the class.
4. A
B
A
B
OBJECT WITH TWO NORMAL
DATA MEMBERS
OBJECT WITH ONE NORMAL
AND ONE STATIC DATA MEMBER
5. 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.
6. 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;
}
};
7. 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();
}