How to use C language object-oriented and C language object-oriented
We all know that C ++ is an object-oriented language, but can C use object-oriented functions?
(1) Inheritance
1 typedef struct _parent2 {3 int data_parent;4 }Parent;5 typedef struct _Child6 {7 struct _parent parent;8 int data_child;9 }Child;
When designing C language inheritance, we need to place the basic data at the beginning of the inherited structure. In this way, data access, strong data conversion, and data access will not be any problem.
(2) Encapsulation
By default, class members are private, while struct members are public (cannot be changed). So how can we enable C language to implement encapsulation? The answer is the function pointer, which is widely used in the kernel;
1 struct _Data;2 typedef void (*process)(struct _Data* pData);3 typedef struct _Data4 {5 int value;6 process pProcess;7 }Data;
Encapsulation means that functions and data are tied together, and data are tied together. In this way, we can access all the data through a simple structure pointer and traverse all the functions. Encapsulation, which is a property of a class and of course a property of a data structure.
(3) polymorphism
In C ++, polymorphism is usually implemented using virtual functions, but there is no virtual function in C language. How can we implement overload?
The answer is also obvious, and it is also an extension of the function pointer. The following example shows an example:
1 # include <stdio. h> 2 # include <stdlib. h> 3 4 // virtual function table structure 5 struct base_vtbl 6 {7 void (* dance) (void *); 8 void (* jump) (void *); 9 }; 10 11 // base class 12 struct base 13 {14/* virtual table */15 struct base_vtbl * vptr; 16}; 17 18 void base_dance (void * this) 19 {20 printf ("base dance \ n"); 21} 22 23 void base_jump (void * this) 24 {25 printf ("base jump \ n "); 26} 27 28/* global vtable for base */29 struct base_vtbl base_table = 30 {31 base_dance, 32 base_jump 33 }; 34 35 // base class constructor 36 struct base * new_base () 37 {38 struct base * temp = (struct base *) malloc (sizeof (struct base )); 39 temp-> vptr = & base_table; 40 return temp; 41} 42 43 44 // derived class 45 struct derived1 46 {47 struct base super; 48/* derived members */49 int high; 50}; 51 52 void derived1_dance (void * this) 53 {54/* implementation of derived1's dance function */55 printf ("derived1 dance \ n"); 56} 57 58 void derived1_jump (void * this) 59 {60/* implementation of derived1's jump function */61 struct derived1 * temp = (struct derived1 *) this; 62 printf ("derived1 jump: % d \ n ", temp-> high); 63} 64 65/* global vtable for derived1 */66 struct base_vtbl derived1_table = 67 {68 (void (*) (void *) & derived1_dance, 69 (void (*) (void *) & derived1_jump 70}; 71 72 // constructor 73 struct derived1 * new_derived1 (int h) of the derived class) 74 {75 struct derived1 * temp = (struct derived1 *) malloc (sizeof (struct derived1); 76 temp-> super. vptr = & derived1_table; 77 temp-> high = h; 78 return temp; 79} 80 81 82 83 int main (void) 84 {85 86 struct base * bas = new_base (); 87 // The member functions of the base class are called here. 88 bas-> vptr-> dance (void *) bas); 89 bas-> vptr-> jump (void *) bas); 90 91 92 struct derived1 * child = new_derived1 (100 ); 93 // The base class Pointer Points to the derived class 94 bas = (struct base *) child; 95 96 // The calling here is actually the member function 97 bas-> vptr-> dance (void *) bas) of the derived class ); 98 bas-> vptr-> jump (void *) bas); 99 return 0; 100}
In conclusion, the Object-Oriented function of C language can be implemented;