C++ Syntax Note - Object-Oriented Model (Memory Distribution of Instances)

Keywords: PHP

Object-Oriented Model (Memory Distribution)
  • For an object, member variables and member functions are stored separately

    • Membership functions are located in code segments, and all class objects are common.

    • Membership variables are unique to each object and are in memory

  • Class objects are distributed in memory exactly the same as struct

  • For inheritance, the object of the subclass is based on the object of the parent class, plus the member of the subclass itself.

 1 #include <iostream>
 2 #include <string>
 3 
 4 using namespace std;
 5 
 6 class Test
 7 {
 8     int mi;
 9 public:
10     Test(int i = 0)  //Conversion constructor
11     {
12         mi = i; 
13     }
14     
15     void showMI()
16     {
17         cout<<"Test mi = "<< mi <<endl;
18     }
19 };
20 
21 class Test1: public Test
22 {
23     int mj;
24 public:
25     Test1(int j = 0)
26     {
27         mj = j;
28     }
29     
30     void showMJ()
31     {
32         cout<<"Test mj = "<< mj <<endl;
33     }
34 };
35 
36 struct T    //T Structure and Test2 The memory structure is exactly the same
37 {
38     int mi;
39     int mj;
40 };
41 
42 int main()
43 {   
44     Test2 t;
45     
46     T *p = reinterpret_cast<T *>(&t);
47     p->mi = 1;
48     p->mj = 2;
49     
50     t.showMI();
51     t.showMJ();
52     
53     return 0;
54 }

 

  • If there is a virtual function in the class, then there is a pointer to the virtual function class table in each object.
 1 #include <iostream>
 2 #include <string>
 3 
 4 using namespace std;
 5 
 6 class Test
 7 {
 8     int mi;
 9 public:
10     Test(int i = 0)  //Conversion constructor
11     {
12         mi = i; 
13     }
14     
15     virtual void showMI()
16     {
17         cout<<"Test mi = "<< mi <<endl;
18     }
19 };
20 
21 class Test1: public Test
22 {
23     int mj;
24 public:
25     Test1(int j = 0)
26     {
27         mj = j;
28     }
29     
30     virtual void showMJ()
31     {
32         cout<<"Test mj = "<< mj <<endl;
33     }
34 };
35 
36 struct T    //T Structure and Test2 The memory structure is exactly the same
37 {
38     void *p;
39     int mi;
40     int mj;
41 };
42 
43 int main()
44 {   
45     Test2 t;
46     
47     T *p = reinterpret_cast<T *>(&t);
48     p->mi = 1;
49     p->mj = 2;
50     
51     t.showMI();
52     t.showMJ();
53     
54     return 0;
55 }

 

  • For multiple inheritance (not recommended), superimpose the parent class in the order declared at the time of inheritance, and eventually fall over the child class's own members.
 1 #include <iostream>
 2 #include <string>
 3 
 4 using namespace std;
 5 
 6 class Test
 7 {
 8     int mi;
 9 public:
10     Test(int i = 0)  //Conversion constructor
11     {
12         mi = i; 
13     }
14     
15     virtual void showMI()
16     {
17         cout<<"Test mi = "<< mi <<endl;
18     }
19 };
20 
21 class Test1
22 {
23     int mj;
24 public:
25     Test1(int j = 0)
26     {
27         mj = j;
28     }
29     
30     virtual void showMJ()
31     {
32         cout<<"Test mj = "<< mj <<endl;
33     }
34 };
35 
36 class Test2 : public Test, public Test1
37 {
38     int mk;
39 public:
40     Test2(int k = 0) 
41     {
42         mk = k;
43     }
44     
45     void showMK()   //Adding virtual function
46     {
47         cout<<"Test mk = "<< mk <<endl;
48     }
49 };
50 
51 struct T    //T Structure and Test2 The memory structure is exactly the same
52 {
53     void *p;   //An additional pointer to the virtual function table
54     int mi;
55     void *p1;  //An additional pointer to the virtual function table
56     int mj;
57     int mk;
58 };
59 
60 int main()
61 {   
62     Test2 t;
63     
64     T *p = reinterpret_cast<T *>(&t);
65     p->mi = 1;
66     p->mj = 2;
67     p->mk = 3;
68     
69     t.showMI();
70     t.showMJ();
71     t.showMK();
72     
73     return 0;
74 }

Posted by dragonfly4 on Wed, 09 Oct 2019 00:35:25 -0700