3, static variables and functions

1, global variables

#include <the iostream>
 the using  namespace STD; 

int a = . 1 ; 

void FUNC ( int & a ) 
{ 
    a ++;                          // Self-add operation 
    COUT a << << endl;            // print the value of a 
} 

int main () 
{ 
    FUNC (A);                      // call 
    FUNC (A); 
    FUNC (A); 
    return  0 ; 
}

 Note : In the function declaration is that we must add '&', or a value does not change. Or pointer

Analysis: program variables defined outside of a main function as a global variable, increasing the value of the print. Global variables have a global scope, the definition of global variables in a source file, the source file can access all the variables, global variables until the end of the program was only destroyed.

2, local variables

#include<iostream>
using namespace std;


int main()
{

    int a;
    a = 4;
    cout << a << endl;
    {
        int b = 1;
        cout << b << endl;
    }
    cout << b << endl;
    return 0;
}

 

 

Analysis: local variables from the scope of the definition, to the end of the {}

The same local variables can not be accessed across function.

#include<iostream>
using namespace std;

void func()
{
    cout << a << endl;
}

int main()
{

    int a;
    a = 4;
    func();
    return 0;
}

 

Analysis: a declared and defined in the main function, but not passed to func (), the syntax error occurs.

3, static variables

#include <the iostream>
 the using  namespace STD; 

static int a = . 1 ; 

void FUNC ( int & a ) 
{ 
    a ++;                          // Self-add operation 
    COUT a << << endl;            // print the value of a 
} 

int main () 
{ 
    FUNC (A);                      // call 
    FUNC (A); 
    FUNC (A); 
    return  0 ; 
}

 

Declare a static variable plus a variable static keyword in front of the ordinary

Static variables and global variables stored in the same manner, except that: a non-static global variable scope is the whole source, when a source program is composed of a plurality of source files, declared non-static global variable in each source file They are effective. The static global variables are restricted in its scope, is only valid in the source file in its statement, it can not be used in the same source program.

4, static variables in the application in c ++

For example, a student of class

#include <the iostream> 
#include < String >
 the using  namespace STD; 

class Student 
{ 
public : 
    Student ( String name, int Age, a float Store, int COUNT);               // declare Constructor 
    ~ Student ();                                               // declare destructor 
    int the GetCount ();
 Private :
     String name;
     int Age;
     a float Store;
     int count;                                           //记录学生数量
    
};
Student::Student(string name, int age, float store,int count)
{
    this->name = name;
    this->age = age;
    this->store = store;
    this->count = count;
    cout << "Student()................" << endl;
}
Student::~Student()
{
    cout << "~Student()............." << endl;
}
int Student::GetCount()
{
    return this->count;
}
int main()
{
    Student stu1("jiang", 18, 99.3,1);
    cout << stu1.GetCount() << endl;
    Student stu2("wang", 19, 100,2);
    cout << stu2.GetCount() << endl;
    cout << endl << endl;

    return 0;
}

 

Analysis: This program count declare private members of a class. Since access to the class. count to record the number of students in the program, and manually entered values ​​count does not make sense.

           Therefore, the constructor call, count ++, call destructors, count--;

           Also note that the variable does not need to count since the release stu1 disappear, that count data belonging to a member of the student class .

So the count is defined as static data members.

The following program with some modifications

#include <the iostream> 
#include < String >
 the using  namespace STD; 

class Student 
{ 
public : 
    Student ( String name, int Age, a float Store);               // declare Constructor 
    ~ Student ();                                               // declare destructor 
    int the GetCount ( );
 Private :
     String name;
     int Age;
     float Store;
     static  int COUNT;                                         //Define static data member variable 
    
}; 
Student Student :: ( String name, int Age, a float Store) 
{ 
    the this -> name = name;
     the this -> = Age Age;
     the this -> Store = Store; 
    COUNT ++;                                                   // COUNT ++ 
    << COUT " Student () ................ " << endl; 
} 
Student :: ~ Student () 
{ 
    COUNT -;                                                  // count--
    << COUT " ~ Student () ............. " << endl; 
} 
int Student :: the GetCount () 
{ 
    return  the this -> COUNT; 
} 
int Student :: COUNT = 0 ;
 int main () 
{ 
    Student * = STU1 new new Student ( " Jiang " , 18 is , 99.3 );           // one by one to observe the release of a pointer to the value of count statement, conveniently after 
    COUT << stu1-> the GetCount () << endl; 
    Student * STU2 = new new Student ( "wang", 19, 100);
    cout << stu2->GetCount() << endl;
    cout << endl << endl;
    
    delete stu1;
    cout << stu2->GetCount() << endl;
    delete stu2;
   cout << Student::GetCount() << endl;

    return 0;
}

 

 

 

Program:

 

 

 

 

 Analysis: In the program, a definition of each STU, count is incremented; dlete only after, count minus 1;

Static data members of the class syntax:

a, since the program is part of the static type, i.e. it can not be defined based on the object; needs to be defined in the outer main function

int Student::count = 0;

 

b, it can be accessed by class name

cout << Student::count << endl;

 

But because it is a private member, and therefore can not be accessed directly, we need to use static functions.

5, static function

To the above program as an example

Analysis: static variables, it also belongs to the class of functions, and can also be accessed via the class name. Static member function can not call an ordinary member functions and member variables common, because the static member functions belong to the class, do not know the common property of the members belong to which objects can only be called a static resource class.

6, the difference between static variables and dynamic variables

a, static variables when the variable is usually defined on sub-set storage unit and remains unchanged until the end of the process. Static variables, global variables are dynamic static storage.

B, in the dynamic variables during program execution, it is used when allocating the storage unit, immediately released after use.

c, the static variable is initialized only once, repeat initialization syntax error occurs. If the static variable is not initialized, the system is automatically defined as 0, and the dynamic variable to a random value.

d, global data: static data, global variables, const constants.

     Heap: new out by the programmers themselves dynamic data needs to be manually released. If you forget the release, will be a memory leak, the end of the program will be recovered by the operating system.

     Stack Area: internal variables function, automatically released at the end assigned automatically by the IDE.

Guess you like

Origin www.cnblogs.com/aaakihi/p/11605548.html