Hi friends,
here we see how static member variable and function are useful in c++.
The fact static members is that only one copy of static variable is created within class no matter how many object of that class is created, class that derived from class which contains static member variables also share the same single copy os static variable.
Most popular use of static is to count how many object of class is created:
To declare static variable syntax are like this:
here we see how static member variable and function are useful in c++.
The fact static members is that only one copy of static variable is created within class no matter how many object of that class is created, class that derived from class which contains static member variables also share the same single copy os static variable.
Most popular use of static is to count how many object of class is created:
To declare static variable syntax are like this:
static int y;
Static function are not frequently used in real life programs because static function can only access static data of
class therefore it has no so much scope within class. syntax for static function are like this:
static void show();
However to access static function or variable we not need object of class, we can do it by using class name and scope resolution opperator :: , see below statement:
int sample :: y;
Here in red color name is class name and in green :: is scope resolution operator.
TO DOWNLOAD SOURCE FILE FOR STATICFUN.CPP program CLICK:
staticfun.cpp
Now output of the program look like this:
Here we can see after creating five object of class the value of ' static int y' is incremented five times, and no matter we have accessed y from object from first object that 's' the of 'y' in second line of output is 5. this shows us that only one copy of 'y' is used in all of the five objects.
So that's all basics about static keyword in c++.
IF you like to see any other tutorial in c++, c,java, vb.net,visual basic,ms access see my site:
