c class 博客分类: c language cclassobject
程序员文章站
2024-03-26 12:19:59
...
/* simple_cat.c */ #include <stdio.h> #include <stdlib.h> #include <string.h> #define MAX 10 typedef struct Animal{ void *this; char name[MAX]; void (*shout)(struct Animal *); }Animal; typedef struct Cat{ Animal *animal; }Cat; void shout_cat(Animal *cat) { printf("%s miaomiao. \n", cat->name); } void visit_animal(Animal *animal) { animal->shout(animal); } void initialize_cat(Cat* cat, char *name) { cat->animal = (Animal *) malloc(sizeof(Animal)); cat->animal->this = cat; cat->animal->shout = shout_cat; strcpy(cat->animal->name, name); } void finalize_cat(Cat *cat) { free(cat->animal); } void main() { Cat cat; initialize_cat(&cat, "Kitty"); visit_animal(cat.animal); finalize_cat(&cat); }
写道
Kitty miaomiao.
Kitty cannot swim.
Kitty cannot swim.
#include <stdio.h> #include <stdlib.h> #include <string.h> //#include <conio.h> #define MAX 10 typedef struct Animal{ void *this; char name[MAX]; void (*shout)(struct Animal *); }Animal; typedef struct Swimmable{ void *this; void (*swim)(struct Swimmable *); }Swimmable; typedef struct Dog{ Animal *Animal; Swimmable *Swimmable; }Dog; typedef struct Cat{ Animal *Animal; Swimmable *Swimmable; }Cat; void shout_Cat(Animal *cat){ printf("%s miaomiao.\n", cat->name); } void swim_Cat(Swimmable *cat){ printf("%s cannot swim.\n", ((Cat *)(cat->this))->Animal->name); } void shout_Dog(Animal *dog){ printf("%s wangwang.\n", dog->name); } void swim_Dog(Swimmable *dog){ printf("%s can swim.\n", ((Dog *)(dog->this))->Animal->name); } void visit_Animal(Animal *animal){ animal->shout(animal); } void visit_Swimmable(Swimmable *swimmable){ swimmable->swim(swimmable); } void initialize_Dog(Dog* dog, char *name){ dog->Animal = (Animal *) malloc(sizeof(Animal)); dog->Animal->this = dog; dog->Animal->shout = shout_Dog; strcpy(dog->Animal->name, name); dog->Swimmable = (Swimmable *) malloc(sizeof(Swimmable)); dog->Swimmable->this = dog; dog->Swimmable->swim = swim_Dog; } void initialize_Cat(Cat* cat, char *name){ cat->Animal = (Animal *) malloc(sizeof(Animal)); cat->Animal->this = cat; cat->Animal->shout = shout_Cat; strcpy(cat->Animal->name, name); cat->Swimmable = (Swimmable *) malloc(sizeof(Swimmable)); cat->Swimmable->this = cat; cat->Swimmable->swim = swim_Cat; } void finalize_Dog(Dog *dog){ free(dog->Animal); free(dog->Swimmable); } void finalize_Cat(Cat *cat){ free(cat->Animal); free(cat->Swimmable); } void main(){ Dog dog; Cat cat; //clrscr(); initialize_Dog(&dog, "Wangcai"); initialize_Cat(&cat, "Kitty"); visit_Animal(dog.Animal); visit_Animal(cat.Animal); visit_Swimmable(dog.Swimmable); visit_Swimmable(cat.Swimmable); finalize_Dog(&dog); finalize_Cat(&cat); }
写道
Wangcai wangwang.
Kitty miaomiao.
Wangcai can swim.
Kitty cannot swim.
Kitty miaomiao.
Wangcai can swim.
Kitty cannot swim.
推荐阅读
-
c class 博客分类: c language cclassobject
-
代码重构经验总结 博客分类: 代码架构 设计模式框架CC++C#
-
代码重构经验总结 博客分类: 代码架构 设计模式框架CC++C#
-
wxWidgets编译注意事项 博客分类: wxWidgets零散技术 C++11wxWidgets
-
在exe程序中嵌入图片 博客分类: wxwidgets编程 Bin2C嵌入图片
-
C#/VB.NET 创建Word项目符号列表、多级项目编号列表 博客分类: .NET Word C#.NETWord项目符号/编号列表免费控件
-
C# 合并Excel工作表 博客分类: .NET Excel C#.NETExcel合并工作表Spire.XLS
-
C# 插入Excel页眉页脚 博客分类: .NET Excel C#.NETExcel页眉页脚Spire.XLS
-
VC6.0与VC 2008的不同之-------SetWindowTextW 不能将参数 1 转换为“LPCTSTR 博客分类: c++ VC++
-
general error c101008a: Failed to save the updated manifest to the file 博客分类: c++