Home
Site Map
Useful Tools
Graphing Calc

Scientific Calculator

C++: Static Variables

What a Java Programmer should know about Static Variables

Java Version C++ Static Variables Example Code Download Source Code Below
Coming Soon
	
#include<iostream>

using namespace std;

class Person{

    public:
        int getSSN();
        Person();

    static int SSN;
    static int getNextSSN();

    private:
        int mySSN;

};

//Java Refugess note!!! static variable is INITALIZED outside of class definition
int Person::SSN = 0;

int  Person::getNextSSN(){ return SSN +1 ;}


Person::Person(){
mySSN = ++SSN;
}

int Person::getSSN(){  return mySSN; }

int main()
{ <
Person pete;
cout<<" Pete's SSN : "<< pete.getSSN() <<"\n" <<endl;;
cout<<"\n\n pete.getNextSSN() : "<< pete.getNextSSN() <<" note the difference from Java " <<endl;
cout<<"\n\n Person::getNextSSN() : "<< Person::getNextSSN() << " more similar to the spirit of Java   \n\n\n" <<endl;


}

	
Sitemap