How to use static C ++ Variables

Source: Internet
Author: User

In a class, static members can share data between multiple objects. Using static data members does not undermine the hidden principle, which ensures security. Therefore, static members are shared among all objects of the class, rather than members of an object.

Static data members can save memory because they are public to all objects. Therefore, for multiple objects, static data members can only store one object for sharing. The value of a static data member is the same for each object, but its value can be updated. You only need to update the value of the static data member once to ensure that all objects have the same value after the update, which improves the time efficiency.

The usage and precautions of static data members are as follows:

1. When defining or describing static data members, add the keyword static.

2. static member Initialization is different from general data member initialization. The format of static data member Initialization is as follows:

<Data type> <class name >:< static data member name >=< value>

 

# Include <iostream>
# Include <iomanip>
Using std: cout;
Using std: endl;

Long next ();

Int main (){
For (int I = 0; I <30; I ++ ){
Cout <std: setw (12) <next ();
}
Cout <endl;
Return 0;
}

Long next (){
Static long last = 0;
Last = last + 1;
Return last;
}

Let's look at a statistic average code using static variables.

# Include <iostream>
Using namespace std;
 
Int f (int I );
 
Int main ()
{
Int num;
 
Do {
Cout <"Enter numbers (-1 to quit ):";
Cin> num;
If (num! =-1)
Cout <"average is:" <f (num) <"n ";

} While (num>-1 );
 
Return 0;
}
 
Int f (int I)
{
Static int sum = 0, count = 0;
 
Sum = sum + I;
 
Count ++;
 
Return sum/count;
}

Initialize static members

# Include <iostream>
Using std: cout;
Using std: endl;

Class Box {
Public:
Box (){
Cout <"Default constructor called" <endl;
++ ObjectCount;
Size = width = height = 1.0;
}

Box (double lvalue, double wvalue, double hvalue ):
Length (lvalue), width (wvalue), height (hvalue ){
Cout <"Box constructor called" <endl;
++ ObjectCount;
}

Double volume () const {
Return length * width * height;
}


Int getObjectCount () const {return objectCount ;}

Private:
Static int objectCount;
Double length;
Double width;
Double height;
};
Int Box: objectCount = 0;

Int main (){
Cout <endl;

Box firstBox (17.0, 11.0, 5.0 );
Cout <"Object count is" <firstBox. getObjectCount () <endl;
Box boxes [5];
Cout <"Object count is" <firstBox. getObjectCount () <endl;

Cout <"Volume of first box ="
<FirstBox. volume ()
<Endl;

Const int count = sizeof boxes/sizeof boxes [0];

Cout <"The boxes array has" <count <"elements ."
<Endl;

Cout <"Each element occupies" <sizeof boxes [0] <"bytes ."
<Endl;

For (int I = 0; I <count; I ++)
Cout <"Volume of boxes [" <I <"] ="
<Boxes [I]. volume ()
<Endl;

Return 0;
}

Result:

Box constructor called
Object count is 1
Default constructor called
Default constructor called
Default constructor called
Default constructor called
Default constructor called
Object count is 6
Volume of first box= 935
The boxes array has 5 elements.
Each element occupies 24 bytes.
Volume of boxes [0] = 1
Volume of boxes [1] = 1
Volume of boxes [2] = 1
Volume of boxes [3] = 1
Volume of boxes [4] = 1

Related Article

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.