C-Language object-oriented programming

Source: Internet
Author: User

#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

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.