-
Notifications
You must be signed in to change notification settings - Fork 3
/
oo_test.c
129 lines (109 loc) · 2.13 KB
/
oo_test.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
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
/*** Java example:
class Shape {
protected String color;
public Shape() {
color = "white";
}
public void print() {
System.out.println("color: " + color);
}
}
class Circle extends Shape {
public Circle() {
radius = 10;
}
public getRadius() {
return radius;
}
private int radius;
}
class Square extends Shape {
public Square() {
side_len = 5;
color = "black";
}
public void print() {
System.out.println("side length: " + side_len + " ");
super.print();
}
private int side_len = 5;
}
circle.print(); // color: white
square.print(); // side length: 5 color: black
Shape [] shapes = {new Circle, new Square, new Shape};
// polymorphism:
for (s in shapes) {
s.print();
}
***/
#include <oo.h>
/* Shape class */
class_extend(shape, object)
void (*print)(struct shape *this);
char *color;
class_end(shape, object)
void
shape_print(struct shape *this)
{
printf("color: %s\n\n", this->color);
}
class_template(shape, object)
.print = shape_print,
class_template_end(shape, object)
void
shape_cons(struct shape *this)
{
//SUPER_CONS(object);
this->color = "white";
}
/* Circle class */
class_extend(circle, shape)
int radius;
int (*get_radius)(struct circle *this);
class_end(circle, shape)
int
get_radius(circle *this)
{
return this->radius;
}
class_template(circle, shape)
.get_radius = get_radius,
.poly.print = shape_print,
class_template_end(circle, shape)
void
circle_cons(circle *this)
{
SUPER_CONS(shape);
this->poly.color = "white";
this->radius = 10;
}
/* Square class */
class_extend(square, shape)
int side_len;
class_end(square, shape)
void
square_print(square *this)
{
printf("side length: %d\n", this->side_len);
SUPER(print, shape);
}
class_template(square, shape)
/* overrides .poly.print in shape */
.poly.print = square_print,
class_template_end(square, shape)
void
square_cons(square *this)
{
SUPER_CONS(shape);
this->side_len = 5;
this->poly.color = "white";
}
int
main(void)
{
circle *c = circle_new();
square *sq = square_new();
INVP(c, print); // c->poly.print(c);
INVP(sq, print); // sq->poly.print(sq);
return 0;
}