Static Data Member In C++
We can
define class members static using static keyword. When we
declare a member of a class as static it means no matter how many objects of
the class are created, there is only one copy of the static member.
A static
member is shared by all objects of the class. All static data is initialized to
zero when the first object is created, if no other initialization is present.
Fig: Static Data Member In C++
Program:
#include<iostream.h>
#include<conio.h>
class MCA
{
private:
static int
count;
public :
void
getdata(int);
void
putdata();
};
void
MCA::getdata(int x)
{
count =x;
count x++;
}
void MCA
:: putdata()
{
cout<<
“count=”count;
}
int
MCA::count
{
void main()
{
MCA a1;
a1.putdata();
a1.putdata();
a1.getdata(20);
a1.getdata(30);
a1.putdata();
a1.putdata();
getch();
}
Output:
Count=0
count=0
Count=30
count =30
No comments:
Post a Comment