View Single Post
  #2 (permalink)  
Old August 19th, 2003, 10:49 PM
7stud 7stud is offline
Authorized User
 
Join Date: Jul 2003
Posts: 36
Thanks: 0
Thanked 0 Times in 0 Posts
Default

//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;
}
Reply With Quote