-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathobjective_c.c
61 lines (52 loc) · 1.64 KB
/
objective_c.c
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
/*
* Simulating Classes and Objects in Pure C
*
* This program demonstrates how to simulate class-like behavior using pure C.
* We define a struct "Object" that encapsulates data and a function pointer,
* which acts as a method. Two "methods" (functions) are implemented: method1 and
* method2. Each function takes a pointer to the struct instance ("self") to access
* the encapsulated data.
*
* ASCII Illustration:
*
* +-----------------------+
* | Object |
* +-----------------------+
* | int data | ---> Data member
* | void (*execute)(*) | ---> Function pointer ("method")
* +-----------------------+
*
* Example Usage:
* 1. Create an instance of Object (e.g., obj1).
* 2. Set its data value.
* 3. Assign a function pointer to emulate a method (e.g., method1).
* 4. Call the method via the function pointer.
*
* Expected Output:
* method1, 10
* method2, 20
*/
#include <stdio.h>
struct Object {
int data; // Encapsulated data
void (*execute)(struct Object *self); // Function pointer to emulate a method
};
void method1(struct Object *self) {
printf("%s, %d\n", __FUNCTION__, self->data);
}
void method2(struct Object *self) {
printf("%s, %d\n", __FUNCTION__, self->data);
}
int main() {
// Create two object instances
struct Object obj1, obj2;
// Initialize obj1 and assign method1 to its function pointer
obj1.data = 10;
obj1.execute = method1;
obj1.execute(&obj1);
// Initialize obj2 and assign method2 to its function pointer
obj2.data = 20;
obj2.execute = method2;
obj2.execute(&obj2);
return 0;
}