Static members-Static Variables

PropellerAds

Static members
Class members which are declared as static are known as Static members. Always Static variable shares same memory location.

Syntax:
Static int a;
Static void show();

Static Variables
The data members which are declared static are known as static variables.

#include <iostream> 
using namespace std;
class num
{
static int c;
public:
void count()
{
++c;
cout&lt;&lt;"value of c is";
}
};
int num::c=10;
int main()
{
num obj1,obj2;
obj1.count();
obj2.count();
}
Output:
11
12

Note:
1. Here static data member variables must be initialised otherwise the linker will
generate an error.
2. The memory for static data members is allocated only once
3. Only one copy of static member variable is created for the whole class for any
number of objects. All the objects have common static data members.

Static Member Function
The member function which are declared static are known as static member function. Only
static keyword before a member function makes a member function static.
Static member function can only access static members of the corresponding class.
Note:
Private static function must be invoked using static public function

Try Now – PROGRAMMING IN C++ MCQs