3. Use of complex objects and this pointer
3.1 Dynamic objects and object pointers
3.1.1 Object pointer
◼The object pointer points to the address where the object is stored
◼ Definition and use
Class name * object pointer name;
Object pointer name-> data member;
Object pointer name-> data function;
◼ Advantages: address transfer, high efficiency
3.1.2 Dynamic objects
CCircle * g_pc = NULL; // Define an object pointer void display () { g_pc- > Draw (); // Use the object pointer, the Draw function in CCirle } void keyboard (unsigned char key) { switch (key) { case ' 1 ' : if (g_pc! = NULL) // The use of pointer delete g_pc; // Pour water in the cup and change the cup g_pc = new CCircle ( 400 , 300 , 100 , PURPLE); // Dynamic object break ; // is to create and release space case ' 2 ' : if (g_pc! = NULL) delete g_pc; g_pc = new CCircle ( 500 , 200 , 150 , RED); break ; } }
CCircle * g_c = NULL; int g_n = 0 ; void LoadCircles ( const char * fileName) { FILE * fp = NULL; float x, y, r; int clr; fp = fopen (fileName, " r " ); // Open And read the file, because there are a lot of numbers fscanf (fp, " % d " , & g_n); cout << g_n << endl; g_c = new CCircle [g_n]; // Continuous number series, g_c points to the first address for(int i=0; i<g_n; i++) { fscanf(fp, "%f %f %f %d", &x, &y, &r, &clr); g_c[i].Set(x, y, r, clr); } fclose(fp); } delete []g_c; //new时有【】,delete时也有
3.2 Object references
3.3 Object Array
◼ Array with objects as elements
◼ Definition and initialization
Class name object array name [n];
initialization:
Array name [n] = {class name (initial value 1, initial value 2, ..),
Class name (initial value 1, initial value 2, ...),
… Class name (initial value 1, initial value 2, ...)};
CCircle g_cir[2] = {CCircle(300,200,100,RED),CCircle(200,280,30,BLUE)}; int a[2] = { 1 , 2 }; void display() { // g_pc->Draw(); for(int i=0;i<2;i++) g_cir[i].Draw(); }
3.4 this pointer
◼ A system-defined pointer to the current object (that is, the address of the current object)
this pointer: where you need to move, is to point to the first address of the structure, v0 and v1 use the same member function
v1 and v0 have their own addresses. Although x and y have the same name, the address is different. The same is that there is only one this pointer, so it is everywhere.
* this: returns the current object
3.5 Combined objects
3.5.1 Definition
It is in a class 1 (combined class), called another class 2 (member class): class 2 p (combined object)
class CPoint // Legendary member class { float x; float y; public : CPoint ( float _x = 0 , float _y = 0 ) // initialization { x = _x; y = _y; } float GetX () { return x ;} float GetY () { return y;} void SetX ( float _x) {x = _x;} void SetY ( float_y) {y = _y;} void Move ( float dx, float dy) {x + = dx; y + = dy;} }; class CCircle { CPoint p; // Come, come! Combine objects! unsigned long clr; float r; public : CCircle ( float x, float y, int _r, unsigned long _clr = 0x00FF00 ): p (x, y) { r = _r; // px = x; py = y; ❌is Private, can it be directly operated, if changed to public clr = _clr; //When using p, it will automatically call the constructor with default parameters, so a few more steps, cout << " Circle con1 " << endl; } CCircle (CPoint _p, int _r): p (_p) { r = _r; cout << " Circle con2 " << endl; } double GetArea (); double GetPerimeter (); void Move ( float dx, float dy); void Set ( float x, float y); void Scale (float s); }; // Code reuse: Not only the circle class, triangle class, superstar class can be used, there is only one version of the Move function. Only need to write once void CCircle :: Move ( float dx, float dy) { p.Move (dx, dy); }
■ Member objects call their respective constructors in the order they appear in the declaration of their combined class , rather than in the order in the initialization list
void Set ( float x, float y) {px = x; py = y;} // Inaccessible , private void Set ( float x, float y) {p.SetX (x); p.SetY (y) ;} // Write a function, just visit it in the function