The author wrote in front
Graduated, mechanical dog is too boring
Pick up what you forgot before and continue to learn, starting with Xiaobai
Slowly improve the notes of c + +. Where you write, you will slowly add before and after. The format is a little chaotic. Don't be angry with the reader
As for what depth to learn and what to do in the end, I don't think about it for the time being
Learn to learn?
I hope to help you, criticize and correct, and make progress together
20211024
Pointer
1. Introducing memory understanding
2. Pointer definition
3. Pointer size
4. Null pointer nullptr
5. Field pointer
6. const and pointer
- const modifier pointer - (you can change your girlfriend (address) but your girlfriend's name cannot change (value))
- const modifier value (constant at this time) - (girlfriend's name can be changed)
- const modifies both the pointer and the value (constant)
int a = 10; int* const p = &a;//Modify the address of a. the address cannot be changed //const int* p = &a;// Modify the value of A. the value cannot be changed cout << p << endl; cout << *p << endl; int b=11; //p = &b;// Change the address of B (change the value by changing the address) *p = b;//Change the value of b cout << p << endl; cout << *p << endl;
Pointers and arrays -- pointers access arrays
double arr[] = { 1,23,4,34,53,5,46,45,64,6,23,423,42 }; double* p =&arr[0];//Initialize pointing to first address //The following help understand cout << sizeof(arr) << endl;//104 cout << sizeof(arr[0]) << endl;//8 cout << sizeof(double) << endl;//8 cout << sizeof(double*) << endl;//4. The size of the pointer depends on the number of bits of the compiler. Here is a 32-bit compiler, 4 bytes cout << sizeof(p) << endl;//4 cout << endl; for (int i = 0; i < 20; i++) { cout << *p << endl; p++;//The address is incremented by the corresponding byte size (double is 8 bytes) }
Pointers and functions -- value passing and address passing
Structures: custom data types
Structure definition: struct structure name {structure member list};
Where: structure member list = data type 1, member name 1; Data type 2 member name 2;
Create structure variable: (struct can be omitted)
1. struct structure name structure variable name; (not initialized, members are initialized separately)
Individual initialization of members: structure variable name. Structure member name = initial value;
2. struct structure name structure variable name = {initial value}; (initialization)
3. Create structure variables when defining structures (use them sparingly and understand them)
struct structure name {structure member list} structure variable name; (not initialized)
struct structure name {structure member list} structure variable name = {initial value}; (initialization)
//Structure definition struct student { //Member list string name; //full name int age; //Age }stu3; //Structure variable creation method 3 int main() { //Structure variable creation method 1 struct student stu1; //The struct keyword can be omitted stu1.name = "Zhang San"; stu1.age = 18; cout << "full name:" << stu1.name << " Age:" << stu1.age << endl; //Structure variable creation method 2 struct student stu2 = { "Li Si",19,60 }; cout << "full name:" << stu2.name << " Age:" << stu2.age << endl; //Structure variable creation method 3: initialize the structure separately stu3.name = "Wang Wu"; stu3.age = 18; stu3.score = 80; cout << "full name:" << stu3.name << " Age:" << stu3.age << endl; system("pause"); return 0; }
Structure array:
struct ss { string name; int age; }; ss ss_arr[2] = { {"her",23}, {"he",21} }; for (int i = 0; i < 2; i++) { cout<<"name: " << ss_arr[i].name << "\t"<<"age: " << ss_arr[i].age << endl; }
Structure pointer
Understanding 1:
//Define structure struct ss{ string name; int age; }; //Create structure variable ss her = { "her",23}; ss he = { "he", 21 }; //Print the contents of structure variables (member information) cout << he.name<< "\t" << he.age << endl; cout << her.name << "\t" << her.age << endl; //Access by pointer struct ss* p1 = &her; string *p2 = &her.name; cout << p1 << endl; cout << p2 << endl;
Understanding 2:
Code symmetry, easy to understand
struct ss { string name; int age; }s1;//Not initialized s1 = { "s1", 17 };//Separate initialization sturct ss* p = &s1;//struct can be omitted /*ss*/ string* p2 = &s1.name;
Nested structure
Code symmetry, easy to understand
//Define student structure struct stduents { string name; int age; }; //Define teacher structure struct teachers { string name;//String variable name int age;//Integer type name, integer variable name struct stduents s1;//struct (can be omitted) students is the structure type, and s1 is the structure variable name (s1 of the teacher's students) }; //Create student structure variable stduents s1 = { "bill",18 };//students is the structure type and s1 is the structure variable name //Create teacher structure variable teachers t1 = { "herry",23,s1 }; cout << t1.name << endl; cout << t1.age << endl; cout << t1.s1.name <<" " << t1.s1.age << endl;
Multiple students can replace the student structure with a structure array, as shown below
I tried, not for the time being
//Define student structure struct stduents { string name; int age; }; //Define teacher structure struct teachers { string name;//String variable name int age;//Integer type name, integer variable name struct stduents ss[];//struct students is the structure type, and ss[2] is the structure variable name (the teacher's two students ss[0] and ss[1]) }; //Create student structure variable stduents ss[2] = { "bill",18,"ada",19};//students is the structure type and s1 is the structure variable name //Create teacher structure variable teachers t1 = { "herry",23,ss[2] }; cout << t1.name << endl; cout << t1.age << endl; cout << t1.ss[0].name <<" " << t1.ss[0].age << endl; cout << t1.ss[1].name <<" " << t1.ss[1].age << endl;
Supplement 1:
err: ambiguous "cout" in c + +
ans: delete the using namespace std and re-enter it
Supplement 2:
qus: what is a destructor?
ans:
Structure as function parameter
pass by value
//Define student structure struct stduents { string name; int age; }; void printstudents(struct stduents ss){ cout << ss.name <<" " << ss.age << endl; } int main() { //Create student structure variable struct stduents ss1 = { "bill",18 };//students is the structure type and ss1 is the structure variable name struct stduents ss2; ss2.name = "ada"; ss2.age = 19; printstudents(ss1); printstudents(ss2); }
Address delivery
//Define student structure struct stduents { string name; int age; }; void printstudents(struct stduents* ss_p){ cout <<ss_p->name <<" " << ss_p->age << endl;//Use - > to access the members of the structure in the address } int main() { //Create student structure variable struct stduents ss1 = { "bill",18 };//students is the structure type and ss1 is the structure variable name struct stduents ss2; ss2.name = "ada"; ss2.age = 19; printstudents(&ss1); printstudents(&ss2); }