Static member data static object initialization.

Source: Internet
Author: User
Tags class definition

1. The definition of static member data, as defined by statically typed variables, to add the keyword static before the definition of the member data.

2. Static member data must have a definite value, but because the member data cannot be initialized directly in the definition of the class, the static member data must be declared again and initialized on the outside of the class definition, without the need to add the keyword static before. At the same time, in order to maintain the consistency of data values for static members, it is generally not necessary to set the initial value of static member data in the constructor of the class. The order in which static member data is initialized is the order in which they are declared outside the class body.

3. In the same program, when a class has multiple objects, the static member data in these objects shares the same storage space, that is, when the class is defined, the static member data is allocated to the storage unit, and when the object of the class is created, the storage unit is no longer allocated for that static member data, and the static member data is not initialized.

Static member Data Initialization format:

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

4. The static member data of a class has some characteristics of a global variable, such as allocating storage space for static member data when the program starts running, but it has only the scope of the class. That is, before executing main (), the storage space is first allocated and initialized for static member data and global variables, and static member data and global variables are undone when the entire program ends.

5. Static member data can also be divided into public, private, and protected static members.

For public static members, which can be accessed through objects of the class, or directly through the class name (this is another difference between static member data and general member data). Format:

< class name >::< static member data >

Private and protected static member data can only be accessed by the public member function of the class.

6. It is noteworthy that the static member data for the class already exists and can be cited before any object is created.

7. Static member data can also be data from a user-defined type.

1. 2 Static member functions

1. When you define a static member function, you can only use the keyword static modifier before the name of the member.

2. A static member function belongs to the entire class, which is a member function that is shared by all objects of the class, and it does not belong to an object. Therefore, it does not contain an implied *this pointer parameter, so it cannot directly access Non-static members (member functions and member data) in an object as normal member functions do.

Static member functions can only access static members of the class (member functions and member data), global variables, external functions, and so on. (because they do not belong to any particular object).

3. Static member functions to access non-static members, you must use the object of the class (the object name or the function argument that points to the object).

4. A static member function is first a member function that can be defined as an inline function or defined outside of the class body, but it is not necessary to add the keyword static before the name of the functor.

5. Static member functions (public) can be accessed through the objects of the class, and can be accessed directly through the class name, in the form of:

< class name >::< static member function name > (< real parameter list >)

6. A static member function cannot be a const member function.

The definition and application of static member data of Example 1

# include <iostream.h>

# include<stdlib.h>

Class Ccounter

{static int count;

int objnumber; Represents an object number

Public:

Ccounter ()

{count + +; objnumber=count;}

void Show ()

{cout<< ' obj ' <<objnumber<< '/t ' << ' count= ' <<count<< '/n ';}

} ;

int ccounter::count=0;//a declares static member data outside of the class definition and initializes it, if the initial value is not assigned,

You can not assign an initial value, this time the system will automatically assign the initial value 0.

void Main ()

{Ccounter obj 1;

Obj1. Show ();

cout<< "----------------------/n";

Ccounter Obj2;

Obj1. Show ();

Obj2. Show ();

cout<< "----------------------/n";

Ccounter obj3;

Obj1. Show ();

Obj2. Show ();

Obj3. Show ();

}

Execution results:

Obj1 count=1

----------------------

Obj1 count=2

Obj2 count=2

----------------------

Obj1 count=3

Obj2 count=3

Obj3 count=3

Note that the static member data for the class already exists and can be referenced before any objects are created.

Example 2 analysis of the results of the program output

Static member data for a class has some characteristics of a global variable, and before executing main (), the static member data and global variables are first allocated storage space and initialized, and static member data and global variables are undone when the entire program ends, but it has only the scope of the class.

Note that the static member data for the class already exists and can be referenced before any object is created

# include <iostream.h>

Class A

{int i;

static int x;

Public:

static int y;

A (int a, int b, int c)

{i=a, x=b, y =c;}

void print ()

{cout<< "i=" <<i<< '/t ' << "x=" <<x<< '/t ' << "y=" <<Y<<ENDL;}

} ;

int A:: x=0;

int A:: y=0;

void Main ()

{Count << ' y= ' <<a:: Y<<endl//b static member data in a class already exists before any object is created

A A (11,22,33);

A.print ();

A B (100, 200, 300);

A.print ();

B.print ();

a::y=400; C, private static member data cannot be directly assigned.

B.print ();

Execution results:

Y=0

i=11 x=22 y=33

i=11 x=200 y=300

i=100 x=200 y=300

i=100 x=200 y=400

Example: Lifetime of static member data

Static member data can also be data from a user-defined type.

#include <iostream.h>

Class A

{int i;

Public:

A (int x) {i=x; cout<< "x=" <<i<< "/T call constructor A ()/n";}

~ A () {cout<< "x=" <<i<< "/T calls destructor ~a ()/n";}

} ;

Class B

{Static A;

static A C;

Public:

B () {cout<< "call constructor B ()/n";}

~b () {cout<< "Call destructor ~b ()/n";}

};

A B:: A (a);//c declares static member data outside of the class body and initializes the

A B:: C (5);//d

A A1 (20); Defines a global variable for a user-defined type and initializes it.

void Main ()

{cout<< ' main () function begins. /n ";

b b;

cout<< the main () function ends. /n ";

}

Execution results:

X=10 Call constructor A ()

X=5 Call constructor A ()

X=20 Call constructor A ()

The main () function starts

Call Constructor B ()

End of main () function

Invoke Destructors ~b ()

X=20 invoke the destructor ~a ()

X=5 invoke the destructor ~a ()

x=10 invoke the destructor ~a ()

Attention:

Before executing main (), the static member data and global variables are first allocated storage space and initialized, and static member data and global variables are undone when the entire program ends.

The order in which static member data is initialized is the order in which they are declared outside the class body, such as reversing line C and line D, the lines 1th and 2nd of the output will be reversed, and the last two lines will be reversed.

Example 3: Definition and use of static member functions

# include <iostream.h>

void Num_show ();

Class Ccomputer

{Float price;

static float total;

Public:

static int num;//number of objects created

Ccomputer (float i)

{price = i; total+= i; num++;}

void Show ()

{cout<< "The computer Price is:" <<price<<endl}

The static void T_show ()//static member function

{num_show ();

cout<< "Total Price is:" <<total<<endl//Access static member data total

}

} ;

float ccomputer::total=0;

int ccomputer::num=0;

void Num_show ()//Output class Ccomputer static data member num

{cout<< "Total number is:" <<ccomputer::num<<endl}

void Main ()

{ccomputer:: T_show ();//Direct access to static member functions via class name

Ccomputer C1 (3500);

C1.show ();

C1.t_show ();

Ccomputer C2 (4500);

C2.show ();

Ccomputer::t_show (); A direct access to static member functions through class names

}//C1. T_show (); C2. T_show ();

Execution results:

Total number is:0

Total Price is:0

The computer price is:3500

Total number Is:1

Total Price is:3500

The computer price is:4500

Total number Is:2

Total Price is:8000

The A line accesses the public static member function in the class name, which is equivalent to any of the following statements:

C1. T_show ();

C2. T_show ();

Example 4: Non-static members of a class are accessed through objects in a static member function

Static member functions can directly invoke other static members of the owning class, but cannot directly access Non-static members (member functions and member data), and to access non-static members, you must use the object of the class.

#include <iostream.h>

Class A

{int x;

static int y;

Public

A (int x1, int x2)

{x=x1; y=y+x2;}

static void Show1 ();

static void Show2 (a);

};

void A::show1 ()

{cout<< "y=" <<y<<endl} Direct access to static data

void A::show2 (a)//

{cout<< "x=" << A. x << "T" << "y=" <<Y<<ENDL;}

{cout<< "x=" <<x<< "T" << "y=" <<Y<<ENDL;} B wrong, non-static members cannot be accessed directly

int a::y=6;

void Main ()

{A A1 (11, 22);

A1.show1 ();//access by object name

A::show2 (A1); Access by class name

A A2 (33,44);

A::show1 (); Access by class name

A2.show2 (A2); C Access by object name

}

For C, swap with A1.SHOW2 (A2) or a::show2 (A2); to Show1 ();

Execution results:

Y=28

X=11 y=28

y=72

X=33 y=72.

Example 7-6

Z07p226l7-6

Program 7-6.cpp

#include <iostream.h>

Class Claa

{public:

Double x,y;

static int num; public static data members--for all objects to "share"

Used to record how many objects have been generated through the constructor.

Claa () {

x=0; y=0;

num++; Each generation of an object, NUM plus 1

}

Claa (double x0, double y0) {

x=x0; Y=y0;

num++;

}

static void Stafun ()//static function member, output the current value of the static data member num

{cout<< "current_num=" <<num<<endl; }

};

int claa::num=0; Static data members must be initialized outside of the class (using the Class name qualification)

void Main ()

{Claa obj (1.2, 3.4), *p;

cout<< "claa::num=" <<claA::num<< "T";

Claa::stafun ();

cout<< "obj.num=" <<obj.num< "T";

Obj.stafun ();

Claa A[3]; Describes an array with 3 objects and calls its constructors three times

cout<< "claa::num=" <<claA::num<< "T";

Claa::stafun ();

p = new Claa (5.6, 7.8); Generates a dynamic object *p that calls the constructor

cout<< "claa::num=" <<claA::num<< "T";

Claa::stafun ();

cout<< "p->num=" <<p->num<< "T";

P->stafun ();

}

After the program executes, the screen displays the result:

Claa::num=1 Current_num=1

Obj.num=1 Current_num=1

Claa::num=4 current_num=4

Claa::num=5 current_num=5

P->num=5 current_num=5

Attention:

The data member X in the Claa class, Y and num are public-owned, so that the program is simplified by directly accessing them in main, or (for Non-public data members), when accessing them outside the class, a public member function like GETX () is created.

Http://hi.baidu.com/stringtop/blog/item/8124d361faf3a948ebf8f8ab.html

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.