#include <stdio.h> #include <stdlib.h>/* * * 1. Simple implementation of simulation virtual function table implementation of C language object-oriented design * 2. to implement: function registration call * 3. File Implementation function Registration call *///Package typedef struct _shape shape;//Simulation virtual function Table struct shapeclass{ void (*construct) ( Shape* self) //Constructor void (*destroy) (shape* self); //destructor void (*draw) (shape* self); // Handling Functions };struct _Shape{ struct ShapeClass *kclass; int x,y;};/ /constructor Void shape_construct (shape* self) {q self- >x = 0; self->y = 0; printf ("ShapE construct\n ");} destructor Void shape_destroy (shape* self) { printf ("shape destroy\n"); //to-do destroy self}void shape_draw (shape* self) { printf ("draw:%d,%d\n", self->x,self->y); //to-do draw self}//Declaration _ shape_class, is the Register virtual function table Struct shapeclass _shape_class={ shape_construct, The //function name is the address of the function Shape_destroy, Shape_draw,}; Shape* newshape () { shape* s = (shape*) malloc (sizeof (Shape)); s->kclass = &_shape_class; s->kclass-> construct (s); return s;} Void deleteshape (Shape* shape) { shape->kclass->destroy (Shape); free (shape);} Int main () {&NBSP;&NBSP;&NBSP;&NBSP;SHAPE*&NBSP;SHAPE&NBSp;= newshape (); shape->kclass->draw (Shape); Deleteshape (shape);}
C-Language object-oriented programming