# Include <iostream>
Using namespace STD;
Class static {
Public:
Static (INT, Int, INT );
~ Static (){}
Int getsum ();
PRIVATE:
Int X;
Int y;
Int Z;
Static int sum;
};
Int static: Sum = 0;
Static: static (INT newx, int newy, int newz)
{
X = newx;
Y = newy;
Z = newz;
// If the sum value is calculated here, and only the sum value is returned in the getsum () function, the constructor A (, 1) is executed) the sum value is 3.
// Then execute the constructor B (, 2). After that, the sum value is changed to 6, and all sums are 6.
// Sum = x + y + z;
}
Int static: getsum ()
{
Sum = x + y + z;
// When a. getsum () is executed, sum = 3; the returned value is 3; then B. getsum (), sum = 6, and the returned value is 6;
// Sum in object A is also changed to 6, but the return value of A. getsum () remains unchanged.
Return sum;
}
//************************************** **
Class M
{
Public:
M (int );
Static void F1 (M); // a static function has a class parameter.
PRIVATE:
Int;
Static int B;
};
M: m (int)
{
A =;
B + =;
}
Void M: F1 (M m)
{
Cout <"A =" <m. A <Endl; // non-static members in the call class must be called through objects.
Cout <"B =" <B <Endl;
}
Int M: B = 0;
//************************************** ***
Int main ()
{
Static A (, 1), B (, 2); // only the static class constructor is executed. The sub-function getsum () is not executed.
Cout <A. getsum () <Endl; // executes the getsum () function of A and returns the returned value instead of the final static sum value.
Cout <B. getsum () <Endl; // executes the getsum () function of B and returns the returned value instead of the final static sum value.
Cout <A. getsum () <Endl; // outputs the return value of A. getsum () instead of the value of sum.
M: F1 (X1); // objects cannot be called when calling static functions, because static functions do not belong to a specific object.
M: F1 (X2); // It belongs to a class and cannot be called by an object. It can only be called by a class.
Return 0;
}