# # # introduction
C++ is an object-oriented programming language, hence the concept of classes.
The class definition is in the header file, the implementation is in the source file, and for convenience, it’s all in the source file.
### constructor
-
The default no-miserably constructor is created automatically, but is removed once an argument is provided.
-
There are two ways to write a structure.
Class Teacher {public: Teacher(){cout << “no arguments constructor” << endl; } Teacher(char* name, int age){cout << “parameter constructor” << endl; this->name = name; this->age = age; }
private: char* name; int age; };
void main(){ Teacher t1 = Teacher(“nan”,20); Teacher t2(“lu”, 18); system(“pause”); }
#### initializer property list for the constructor
Now there is a Student class with two private members of Teacher. The Student constructor needs to initialize the Teacher class without direct access to the private properties of the Teacher class.
So it can only be called by initializing the list. Equivalent to Java using super(…) The parent class is initialized by calling its constructor.
class Teacher { public: Teacher(char* name, int age){ this->name = name; this->age = age; } Teacher(){ } private: char* name; int age; }; class Student{ public: Student(int id, char* t1_name, int t1_age, char*t2_name, int t2_age) :t1(t1_name, t1_age), t2(t2_name, t2_age) { //t1.name = t1_name; } private: int id;} Teacher private: int id; Teacher t1; Teacher t2; }; void main(){ Student s(1, "wu", 20, "li", 18); system("pause"); }Copy the code
### destructor
Destructor: The destructor is called when an object is about to be released by the system (such as when the stack exits) for cleaning up, such as freeing dynamically allocated memory.
Class Teacher {public: Teacher(){cout << "no arguments constructor" << endl; name = (char*)malloc(sizeof(char)* 100); strcpy(name, "lu"); age = 18; } ~Teacher(){cout << "destructor" << endl; free(name); } private: char* name; int age; }; void fun(){ Teacher t; } void main(){ fun(); system("pause"); }Copy the code
### Copy constructor
-
The default copy constructor is to copy values, as follows.
-
The copy constructor is called in the following scenarios:
If a parameter is passed in as a parameter, it will not be called because it refers to the same piece of memory and there is no copy problem. If a parameter is passed in as a parameter, it will not be called because it refers to the same piece of memoryCopy the code
Examples are as follows:
Class Teacher {public: Teacher(char* name, int age){cout << "with arguments constructor" << endl; this->name = name; this->age = age; } Teacher(const Teacher &obj){cout << "copy constructor" << endl; this->name = obj.name; this->age = obj.age; } private: char* name; int age; }; Teacher fun(Teacher t){ return t; } void main(){ Teacher t1 = Teacher("nan", 20); Teacher t2 = t1; Teacher t3 = fun(t1); system("pause"); }Copy the code
# # # # shallow copy
The default copy constructor implementation is shallow copy, or value copy, as follows:
class Teacher { public: Teacher(){ this->name = (char*)malloc(sizeof(char)* 100); strcpy(name, "lu"); this->age = age; } ~Teacher(){cout << "destructor" << endl; free(name); } Teacher(const Teacher &obj){cout << "copy constructor" << endl; this->name = obj.name; this->age = obj.age; } private: char* name; int age; }; void fun(){ Teacher t1; Teacher t2 = t1; } void main(){ fun(); system("pause"); }Copy the code
Problems with value copying:
If you have dynamic memory allocation, if it’s just a copy of a value, it’s going to be destructed twice.
For example, if a char* name, t1, and T2 all refer to the same address, then the destructor will be free twice, because the same memory address cannot be freed twice, thus raising an exception interrupt.Copy the code
As shown below:
# # # # deep copy
Override the default shallow copy and write a deep copy yourself:
The name of T2 is the newly allocated memory, so T1 and T2 point to two different memory, so they are released separately during the destruction without interfering with each other, which solves the problem of shallow copy.
class Teacher { public: Teacher(){ this->name = (char*)malloc(sizeof(char)* 100); strcpy(name, "lu"); this->age = age; } ~Teacher(){cout << "destructor" << endl; free(name); } Teacher(const Teacher &obj){cout << "copy constructor" << endl; int len = strlen(obj.name); this->name = (char*)malloc(sizeof(char)* (len + 1)); Strcpy (this->name, obj.name); this->age = obj.age; } private: char* name; int age; }; void fun(){ Teacher t1; Teacher t2 = t1; } void main(){ fun(); system("pause"); }Copy the code
As shown below:
Dynamic memory allocation
- C++ uses new and delete for dynamic memory allocation, when construction and destructor are called.
- C uses malloc and free for dynamic memory allocation. Construction and destructor are not called.
Example:
Void main(){cout << "C++ with new and delete dynamic memory allocation "<< endl; Teacher* t1 = new Teacher("wu", 20); delete t1; Cout << "C uses malloc and free for dynamic memory allocation "<< endl; Teacher* t2 = (Teacher*)malloc(sizeof(Teacher)); free(t2); system("pause"); }Copy the code
For an array, add [] :
int* arr1 = (int*)malloc(sizeof(int)* 10);
arr1[0] = 1;
free(arr1);
int* arr2 = new int[10];
arr2[0] = 1;
delete[] arr2;
Copy the code
### class static properties and functions
- The static properties of a class must be initialized globally.
- Non-static, static functions can access static properties, but static functions can only access static properties. (Just like Java)
- Static properties and methods can be accessed directly by the class name :: (if public). It can also be accessed through the object ::.
Example:
class Test { public: void m1(){ count++; } static void m2(){ count++; } private: static int count; }; Int Test::count = 0; void main(){ //Test::count Test::m2(); system("pause"); }Copy the code
## the sizeof the class
C++ memory partition:
Stack heap global (static, global) constant area (string) program code areaCopy the code
Structs have the concept of byte alignment, so the ordinary properties of a class have the same memory layout as the struct.
For example, the following structure is 32 = 4 x 8:
struct C{ public: int i; //8 bytes (double = 8 bytes) int j; //8 bytes int k; //8 bytes double x; //8 bytes void(*test)(); // The pointer is 4 bytes, but stored in the code area};Copy the code
For example, the size of the following class is 24
class A{ public: int i; //8 bytes (double = 8 bytes) int j; //8 bytes int k; //8 bytes double t; //8 bytes static int m; Void test(){cout << "print" << endl; }};Copy the code
### class this pointer is a constant function
- Const comes after the function.
- This pointer class is a pointer constant and cannot be modified.
- With the const modifier added here, the tihs pointer is more of a constant pointer and cannot be changed.
Example:
class Teacher{ private: char* name; int age; public: Teacher(char* name, int age){ this->name = name; this->age = age; // Teacher* const this void myprint() const{printf("%#x\n", this); // Teacher* const this void myprint() const{printf("%#x\n", this); //this->name = "yuehang"; // change the value of this pointer //this = (Teacher*)0x00009; cout << this->name << "," << this->age << endl; }};Copy the code
This, the pointer to the current object
C++ functions are shared (multiple objects share a function in a code area), and there must be a way to identify who the current object is, and that is the this pointer.
For example, when we use structs to simulate classes, we need to customize the this pointer. So, at the time of JNI’s development, JniEnv was a second-class pointer in C (struct implementation, struct has no tihs pointer in C) and a first-class pointer in C++ (class implementation, class has this pointer).
Development:
Java memory partition:
- JVM Stack (Base data types, object references)
- Native Method Stack
- Methods area
- Program counter area
- Direct memory
The basic concept of hot repair: get a pointer to the wrong function, a pointer to the right function. (Both functions are already loaded)
### friend functions
Main functions: data sharing between classes, classes and functions. For example, if the class is already written and you don’t want to change the structure of the class (for example, access permissions), the best way is to use friends.
You can access private attributes of a class in a friend function.
For example, fun is A friend function of class A, so fun has access to A’s private properties.
class Test{ friend void fun(Test &t, int i); Public: Test(int I){this-> I = I; } private: int i; }; Void fun(Test &t, int I){t.i = I; // Void fun(Test &t, int I){t.i = I; } void main(){ Test t(10); fun(t, 20); system("pause"); }Copy the code
Similarly, B is A friend of A, so B has access to A’s private attributes:
class A{
friend class B;
private:
int i;
};
class B
{
public:
void test(){
a.i = 20;
}
private:
A a;
int i;
};
Copy the code
Extension: In Java, Class refers to the “friend Class” of an Object. The Class Class can access any member of an Object.
### operator overload
Operator overloading is essentially a function call.
Now there is a class:
class Point
{
public:
Point(int x, int y){
this->x = x;
this->y = y;
}
public:
int x;
int y;
};
Copy the code
Need to reload ‘+’ :
Point operator+(Point &p1, Point &p2){
return Point(p1.x + p2.x, p2.y + p2.y);
}
Copy the code
Note that if both the x and y attributes of a Point are private, you need to use the friend function. For example, if the class is already written, you don’t want to break the structure of the class:
class Point
{
friend Point operator+(Point &p1, Point &p2);
public:
Point(int x, int y){
this->x = x;
this->y = y;
}
private:
int x;
int y;
};
Point operator+(Point &p1, Point &p2){
return Point(p1.x + p2.x, p2.y + p2.y);
}
Copy the code
If the operator is overloaded inside the class, the class can access its private member directly by omitting a parameter because of the this pointer:
class Point
{
public:
Point(int x, int y){
this->x = x;
this->y = y;
}
Point operator+(Point &p){
return Point(this->x + p.x, this->y + p.y);
}
private:
int x;
int y;
};
Copy the code
If you feel that my words are helpful to you, welcome to pay attention to my public number:
My group welcomes everyone to come in and discuss all kinds of technical and non-technical topics. If you are interested, please add my wechat huannan88 and I will take you into our group.