Prog 25 Write a program for a static member can be used as a counter that count a record occurance of all the object.
#include<iostream.h>
#include<conio.h>
class item
{
public:
static int count;
int no;
void getdata(int a)
{
no=a;
count++;
}
void display()
{
cout<<"\n count ="<<count;
}
};
int item::count;
void main()
{
clrscr();
item A,B,C;
A.display();
B.display();
C.display();
A.getdata(100);
A.display();
B.getdata(200);
B.display();
C.getdata(300);
C.display();
getch();
}
Output:
count =0 count =0 count =0 count =1 count =2 count =3
#include<iostream.h>
#include<conio.h>
class item
{
public:
static int count;
int no;
void getdata(int a)
{
no=a;
count++;
}
void display()
{
cout<<"\n count ="<<count;
}
};
int item::count;
void main()
{
clrscr();
item A,B,C;
A.display();
B.display();
C.display();
A.getdata(100);
A.display();
B.getdata(200);
B.display();
C.getdata(300);
C.display();
getch();
}
Output:
count =0 count =0 count =0 count =1 count =2 count =3
No comments:
Post a Comment