欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页

c class

程序员文章站 2022-07-12 15:20:16
...
/* 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.

 

 

#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.

 

相关标签: c class object