The code shown in the figure, illustrates the use of static data members. In the code, the static variable id is initialized to zero when the objects are created. The id is incremented whenever the data is read into an object. Since, the data is read into the object thrice, the variable id is incremented three times.
Example:
#include <conlo.h>
#include<iostream.h>
class student
{
static int id;
static int mark;
public:
void getdata(int m)
{
mark=m;
id++;
}
void dispdata()
{
cout«"student id"<<" "<<id«endl;
}
};
int student:: id;
int student:: mark;
void main()
{
clrscr();
student s1,s2,s3;
si dispdata():
s2.dispdala():
s3.dispdata():
sl.getdata(78).
s2.getdata(89).
s3.getdata(98).
cout«"Atter getting the data"«endl;
si dispdata():
s2.dispdata():
s3.dispdaia():
getch();
}
The code shown in the figure, illustrates the usage of static member functions. The static function dispid() displays the objects created thus far. The static variable id maintains the count of number of objects created.
Example:
#include <conlo.h>
#nclude<iostream.h>
class stud
{
int i;
static int id;
public:
void setdata()
{
i=++id;
}
void dispi()
{
cout<< "The value of i:"<<" "<<i«endl;
}
static void dispid(void)
{
cout« "The value of id:"«" "<<id«endl:
}
};
int stud:: id;
void main()
{ clrscr();
stud id1.id2;
idl.setdata();
id2.setdata();
stud:: dispid();
stud id3;
stud:: dispid();
id3.setdata();
idl.dispi();
id2.dispi();
id3.dispi();
getch():
}

