美文网首页
C语言实现面向对象

C语言实现面向对象

作者: 草莓2020 | 来源:发表于2020-12-22 20:59 被阅读0次
    person为父类,child为子类
    代码实现如下
    person.h
    typedef struct Person {
    // 模拟类的成员属性
    char *name;
    int age;
    // 模拟类的方法
    void (*showInfo)(Person_T);
    int (*compare)(void *,void *,int (*compare)(void *,void *));
    }*Person_T;
    
    // 模拟对象的创建
    Person_T Person_new(char *name,int age);
    void Person_free(Person_T person);
    
    person.c
    #include"person.h"
    #include<stdio.h>
    #include<stdlib.h>
    
    // 打印属性信息
    void Person_info(Person_T person){
    printf("name:%s age:%d\n",person->name,person->age);
    }
    
    // 信息比较函数
    int Person_compare(void *a,void *b,int (*compare)(void *,void *)){
    return (*compare)(a,b);
    }
    
    // 创建对象并初始化
    Person_T Person_new(char *name,int age){
    Person_T person = malloc(sizeof(struct Person));
    // 初始化成员属性
    person->name = name;
    person->age = age;
    // 初始化方法
    person->showInfo = Person_info;
    person->compare = Person_compare;
    return person;
    }
    
    // 释放指针对内存的引用
    void Person_free(Person_T person){
    free(person);
    }
    
    child.h
    #include"person.h"
    
    typedef struct Child {
    // 模拟类的成员属性
    Person_T person;
    float hight;
    }*Child_T;
    
    // 模拟对象的创建
    Child_T Child_new(char *name,int age,float hight);
    void Child_free(Child_T child);
    
    child.c
    #include"child.h"
    #include<stdio.h>
    #include<stdlib.h>
    
    // 打印属性信息
    void Child_info(Child_T child){
    printf("name:%s age:%d height:%.2f\n",child->person->name,child->person->age,child->hight);
    }
    
    // 创建对象并初始化
    Child_T Child_new(char *name,int age,float hight){
    Child_T child = malloc(sizeof(struct Child));
    // 初始化成员属性
    child->person = Person_new(name,age);
    child->hight = hight;
    // 重写继承父类的showInfo方法
    child->person->showInfo = Child_info;
    return child;
    }
    
    // 释放指针对内存的引用
    void Child_free(Child_T child){
    Person_free(child->person);
    free(child);
    }
    
    test.c
    #include<stdio.h>
    #include<string.h>
    #include"child.h"
    
    // 整型数字的比较函数
    int compareInts(void *a,void *b){
    int *ta = (int *)a;
    int *tb = (int *)b;
    return (*ta - *tb);
    }
    
    int main(){
    
    // 创建对象
    Person_T person = Person_new("mark",20);
    Child_T child = Child_new("jack",21,178.0f);
    
    // 通过对象名调用打印属性信息方法
    person->showInfo(person);
    child->person->showInfo(child);
    
    // 通过对象名调用信息比较方法
    person->compare(person->name,child->person->name,strcmp);
    child->person->compare(&(person->age),&(child->person->age),compareInts);
    
    // 打印比较结果
     printf("%d\n",person->compare(person->name,child->person->name,strcmp));
     printf("%d\n",child->person->compare(&(person->age),&(child->person->age),compareInts));
    
    // 释放指针对内存的引用
     Person_free(person);
     Child_free(child);
    
    return 0;
    }
    

    相关文章

      网友评论

          本文标题:C语言实现面向对象

          本文链接:https://www.haomeiwen.com/subject/kpcenktx.html