c言語オブジェクト向けプログラミング


#include <stdio.h>
#include <stdlib.h>

/*
 *
 *   1.              c         
 *   2.     :        
 *   3.                        
 */

//  
typedef struct _Shape Shape;

//      
struct ShapeClass
{
    void (*construct)(Shape* self); //    
    void (*destroy)(Shape* self);   //    
    void (*draw)(Shape* self);      //      
};

struct _Shape
{
   struct ShapeClass *kclass;
   int x,y;
};

//    
void Shape_construct(Shape* self)
{q
    
     self->x = 0;
     self->y = 0;
     printf("Shape construct
"); } // void Shape_destroy(Shape* self) {     printf("shape destroy
");     //to-do destroy self } void Shape_draw(Shape* self) {     printf("draw:%d,%d
",self->x,self->y);     //to-do draw self } // _shape_class,  struct ShapeClass _shape_class= {     Shape_construct,  //     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() {     Shape* shape = newShape();     shape->kclass->draw(shape);     deleteShape(shape); }