//a.h
class a
{
protected:
static int aa;//declares the static variable
public:
void a::doo();
void display();
};
--------------------------
//a.cpp
#include "test.h"
#include<iostream>
using namespace std;
void a::doo()
{
aa=5;
}
void a::display()
{
cout<<aa<<endl;
}
int a::aa = 0;//defines and initializes the static member
//aa exists before any objects of the class exist
//so it cannot be initialized in a class function.
--------------------
//main.cpp
#include<iostream>
#include "test.h"
using namespace std;
int main()
{
a A;
A.display();
A.doo();
A.display();
return 0;
}
|