1 C++ basic part of the knowledge need to pay attention to

(1) C++ program sub-file writing

Create a.h header file. Create a.cpp source file. Create a.h header file. #include<iostream> using namespace STD; void swap(int a,int b); CPP #include"swap.h" void swap(int a,int b){int temp; temp=a; a=b; b=temp; cout<<a<<endl; cout<<b<<endl; } #include"xug. H "int main(){swap(2,3); return 0; }Copy the code

(2) Q: Pointers are also data types. How many bytes do Pointers occupy?

On a 32-bit operating system, Pointers take 4 bytes on a 64-bit operating system, Pointers take 8 bytes int* char* long*, etc., are pointer typesCopy the code

(3) const modifier

int const * p; Int * const p; int * const p; Int const * const p; int const * const p; Features: The pointer pointer cannot be changed, nor can the value it points toCopy the code


C++ intermediate part of the knowledge to be aware of

2-1 new keyword

int * p = new int(10);
delete p;

int * arr = new int[10];
delete[]arr;
Copy the code

2-2 Reference type

(1) Reference type

#include<iostream> using namespace std; int main(){ int a=10; int &b=a; //b references a cout<<a<<endl; //10 cout<<b<<endl; // a =100; // a =100; // B =100; cout<<a<<endl; //100 cout<<b<<endl; //100 return 0; } Note: 1, the reference must be initialized. 2. A reference cannot be changed after initialization. B can only be a community with A.Copy the code

(2) Reference type as function parameter

#include<iostream> using namespace STD; void swap(int a,int b){ int temp=a; a=b; b=temp; } int main(){ int a=10; int b=20; swap(a,b); // Cout <<a<<endl; //10 cout<<b<<endl; //20 return 0; } #include<iostream> using namespace STD; void swap(int &a,int &b){ int temp=a; a=b; b=temp; } int main(){ int a=10; int b=20; swap(a,b); Cout <<a<<endl; //20 cout<<b<<endl; //10 return 0; } #include<iostream> using namespace STD; void swap(int *a,int *b){ int temp= *a; *a=*b; *b=temp; } int main(){ int a=10; int b=20; swap(&a,&b); Cout <<a<<endl; //20 cout<<b<<endl; //10 return 0; }Copy the code

(3) Reference type to return value of function

#include<iostream> using namespace STD; int & test(){ int a=10; return a; } int main(){ int &b=test(); cout<<b<<endl; //10 cout<<b<<endl; //0 return 0; #include<iostream> using namespace STD; #include<iostream> int & test(){ static int a=10; // Static variable, global area return a; } int main(){ int &b=test(); cout<<b<<endl; //10 cout<<b<<endl; //10 test()=100; cout<<b<<endl; //100 cout<<b<<endl; //100 return 0; }Copy the code

(4) The nature of reference types

The essence of a reference is implemented inside C++ as a pointer constant. int * const p; Features: The pointer pointer cannot be changed, but the pointer value can be changedCopy the code

(5) Constant reference

Used to modify parameters to prevent misoperation.Copy the code

2-3 functions improved

(1) The default parameter of the function

#include<iostream> using namespace std; int fun(int a,int b=20,int c=30){ return a+b+c; } int main(){ cout<<fun(10)<<endl; / / 60 cout < < fun (10 ') < < endl; //100 return 0; } Syntax: return value type function name (data type parameter = default value). 2. If we pass in our own data, use our own, if not, use the default. 3. If a location already has default parameters, then there must be defaults from left to right from that location. 4. If the function declaration and implementation cannot set default parameters at the same time. Purpose: To prevent ambiguity between function declaration and implementation default parameters.Copy the code

(2) function placeholder parameters

In C++, a function's formal argument list can contain placeholder arguments that must be filled when the function is called. Syntax: Return value type function name (data type). Function placeholder Default parameter: Return value type Function name (data type = default value).Copy the code

3 Classes and objects

(1) Classification and call of constructors

Class Person{public: Person(){cout<<"Person parameterless constructor "<<endl; } Person(int a){ age=a; Cout <<"Person with argument constructor "<<endl; } Person(const Person &p){ age=p.age; Cout <<"Person copy argument constructor "<<endl; } ~Person(){cout<<"Person destructor "<<endl; } int age; }; Call: 1, Person person1; // The default constructor Person person2(10); // Person person3(person2); Note: When calling the default constructor, do not write Person person1() without parentheses, because the compiler will treat it as a function declaration; Person1 = Person(); // The default constructor Person person2 = Person(10); Person person3 = Person(person2); Person(person3) = Person person3; Person(person3) = Person person3; Person(person3); Person person4 = 10; Person person4 = Person(10); Construct Person person5 = person4; // copy constructCopy the code

(2) the timing of the copy constructor call

1. Initialize the newly created object by using another already created object of the same type. 2. Copy the object and pass it as an argument to the function. Copy the object and return it from the function.Copy the code

(3) Constructor call rules

By default, the C++ compiler adds at least three functions to a class: (1) a default constructor with no arguments and an empty body; (2) a default destructor with no arguments and an empty body; (3) a default copy constructor; If the user defines a parameter constructor, C++ does not provide a default constructor, but a default copy constructor. If the user defines a copy constructor, C++ does not provide another constructor.Copy the code

(4) Deep copy and shallow copy

Shallow copy: a simple assignment. Deep copy: reapplies for space in the heap for copying. Write your own copy function. In C++, pointer assignments are usually newCopy the code

(5) Initialize the list

Syntax: Constructor (): Attribute 1(value 1), attribute 2(value 2){}Copy the code

(6) Combination of matters needing attention

When other classes are members of this class, the class object is constructed first, before the class itself is constructed.Copy the code

(7) Static member variables

1. All objects share the same data. 2. Memory is allocated at compile time. Java allocates memory at load time. (Java is defined and declared within a class, and initialization is not always possible.) Use: Access object names through objects. Static member variables (public) by the name of the class to access the name of the class: : static member variables (public) static member function...Copy the code

(8) Const decorates member functions

A. We call a const function b after a member function. C. Member attributes cannot be modified in mutable functions by adding the keyword to their declarations: Return value type Function name () const{} Const object: a. B. A const object can only call a const function: read-onlyCopy the code

(9) Friend functions

A friend function of a class is defined outside the class but has access to all private and protected members of the class. Friend functions are not member functions, although their prototypes appear in class definitions. A friend can be a function that is called a friend function; A friend can also be a class, which is called a friend class, in which case the entire class and all its members are friends. Friends of three kinds of implementation: 1, the global function to do friends. Void test1(Person &p){cout<<p.age; } friend void test1(Person &p);} friend void test1(Person &p); Class friends without modifiers. There are two classes Person and Student. If you want Student to access private and protected members of the Person class, you need to add friend class Stduent at the beginning of the Person class. 3. Member functions are friends. If you want A member of the Student class void A() to access the private and protected members of the Person class, you need to declare friend void Student::A(); No modifiersCopy the code

(10) Operator overload

A. For built-in data types, the compiler knows how to perform operations. B. Two ways of operator overloading: member operator function and friend operator function C. Class name ::operator Overloaded operator (argument list) {function body; } friend operator function friend type class name ::operator Overloaded operator (argument list) {function body; }Copy the code

(11) Operator overloading considerations

A. in c++, except for the five operators (. * :: Sizeof? All but one can be reloaded. B. Operator overloading can change the original behavior of an operator, but the precedence, associativity, and number of operands of the operator do not change after overloading. Only existing operators can be overloaded. C. The overloaded functions of the operator are similar to the original functions. At least one parameter of the overloaded function must be of a custom type. D. When overloaded operators (), [], ->, or =, the operator overload function must declare one as a member of the class. For other operators, the operator overload function may be a member function or a friend function.Copy the code
① Plus operator overload

3 advanced parts in C++