Tuesday, September 7, 2010

static data member in c++ | How to use Static Data Member

,
Static data member:-The static is a keyword and are used to retain value of a variable.
When a variable is declared as static it is initialised to zero
.A static function or data element is only recognised inside the scope of the current.
if the local variable is declared with static keyword.it allows the last value of the variable to be retained between continuation calls to that function.
A static data  item is helpful when all the objects of the class should share a common data.
The satic data variable is accessible within the class,but its value remains through the whole program.


class sumnum
{
private:
static int c;
int num;
public:
void input()
{
cout<<"\nEnter number:";

cin>>num;
c=c+num;
}
void sum()
{
cout<<<"the sum"<
}
};
int sumnum:: c=0;
int main()
{
class sumnum a,b,c;
clrscr();
a.input();
b.input();
c.input();
a.sum();
return 0;
}

0 comments to “static data member in c++ | How to use Static Data Member”

Post a Comment