C实现结构体的继承(转)

  
  struct Fruit
  {
   void (*output)(void);
   int i;
  };
  
  struct Apple
  {
   struct Fruit n;
   void (*output)(void);
  };
  
  void output_base(void);
  void output_driven(void);
  void output_apple(void);
  
  int main()
  {
   struct Fruit *p;
   struct Apple *q;
   struct Fruit base;
   struct Apple driven;
   
   base.i = 0;
   base.output = output_base;
   
   driven.n.i = 1;
   driven.n.output = output_driven;/*注意置空会出现什么结果*/
   driven.output = output_apple;
   
   p = &base;
   printf("base's i is %dn",p->i);
   p->output();
   
   p = (Fruit *)&driven;
   printf("driven's i is %dn",p->i);
   
   q = &driven;
   q->output();
   
   getchar();
   return 0;
  }
  
  void output_base(void)
  {
   printf("this is the basen");
  }
  
  void output_driven(void)
  {
   printf("this is the drivenn");
  }
  
  void output_apple()
  {
   printf("this is apple's outputn");
  } 

你可能感兴趣的:(C/C++,output,apple,struct,c)