Static variables and static functions in C ++ class

Source: Internet
Author: User

# 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;
}

 

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.