Article directory
Catalog
I. storage
It can be seen that after static storage class modification, the value of i does not start from the beginning, but remains from the last result
#include <iostream> using namespace std; class Data { public: Data(){} ~Data(){} void show() { cout<<this->data<<" "<<number<<endl; } static void showData()//Exists before the object of a class { //This method is called without this pointer cout<<" "<<number<<endl; } private: int data; public: static int number; //Static data is initialized out of class during declaration }; int Data::number=0;//Static member initialization int main() { Data::showData();//Called directly by class name Data::number = 100;//Use directly by class name Data d; d.show(); d.showData();//Call through object cout << "Hello World!" << endl; return 0; }
Operation result:
0
4197152 100
100
Hello World!
The extern modifier is usually used when a global variable or function is shared by multiple files
II. Operators
#include <iostream> using namespace std; int main () { int var; int *ptr; int val; var = 3000; // Get the address of var ptr = &var; // Get the value of ptr val = *ptr; cout << "Value of var :" << var << endl; cout << "Value of ptr :" << ptr << endl; cout << "Value of val :" << val << endl; return 0; }
Three. Cycle
while
for
for loop statement based on scope
#include <iostream> using namespace std; int main() { int my_array[5] = {1, 2, 3, 4, 5}; // Multiply each array element by 2 for (int &x : my_array) { x *= 2; cout << x << endl; } cout<<endl; // auto type is also in the new standard of C++11, which is used to obtain the type of variable automatically for (auto &x : my_array) { x *= 2; cout << x << endl; } }
Operation result:
2
4
6
8
10
4
8
12
16
20
The first part of the for statement defines the variables used for scope iteration, just like the variables declared in the general for loop, whose scope is only the scope of the loop. The second block after "represents the scope to be iterated.