Static data members of a class have different meanings from common static variables. They mean that each object of A Class shares a bucket instead of a bucket, all objects in this class can directly access the bucket. In fact, it is a variable dedicated for the object of this class-if you declare it as private permission.
To define static data members in a class, you only need to add "static" before the definition ". Static data members of a class can only be initialized outside the class. If they are not initialized, the class is automatically initialized to 0. Static data members referenced outside the class must always be in the form of class name: variable name. Static data members can be used to count how many such objects are created.
For example:
# Include <iostream>
Using namespace STD;
Class AA
{
PRIVATE:
Int;
Public:
Static int count; // defines static members of a class.
AA (int aa = 0) {A = AA; count ++ ;}
// For the class method, if it is relatively simple, you can write it to makeProgramCompact
Int get_a () {return ;}
};
Int AA: Count = 0; // initialization outside the class
Void main ()
{
Cout <"Count =" <AA: Count <Endl;
Aa x (10), y (20 );
Cout <"X. A =" <X. get_a () <"Count =" <X. Count <Endl;
Cout <"Y. A =" <Y. get_a ();
}
We can also define static functions. You only need to add static functions to the front when declaring the function, but do not need to add static functions when defining the function. When using static functions, you also need the form of "Class Name: static member function name (parameter table.