C的对象编程

  1. 封装
#ifndef _COBJ_H
#define _COBJ_H

#ifdef __cplusplus
extern "C"{
#endif

    typedef void* HPERSON;
    typedef struct _Person{
        char* name;
        int age;
        int number;
    }Person;
    HPERSON createPerson(const char* name);
    void setPerson(HPERSON person, int age, int number);
    void displayPerson(HPERSON person);
    void deletePerson(HPERSON person);

#ifdef __cplusplus
}
#endif

#endif

#define _CRT_SECURE_NO_WARNINGS
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include "cobj1.h"


HPERSON createPerson(const char* name){
    printf("createPerson.\n");
    Person* person = NULL;
    person = (Person*)malloc(sizeof(Person));
    person->name = (char*)malloc(strlen(name) + 1);
    strcpy(person->name, name);
    person->age = 0;
    person->number = 0;
    return (HPERSON)person;
}
void setPerson(HPERSON person, int age, int number){
    Person* p = (Person*)person; 
    printf("setPerson.\n");
    if (p != NULL){
        p->age = age;
        p->number = number;
    }
}
void displayPerson(HPERSON person){
    Person* p = (Person*)person;
    printf("\t<<<displayPerson.>>>\n");
    if (p != NULL){
        printf("Name:%s\t Number:%d\t Age:%d\n",
            p->name, p->age,p->number);
    }
}
void deletePerson(HPERSON person){
    Person* p = (Person*)person;
    printf("deletePerson.\n");
    if (p != NULL){
        free(p->name);
        free(p);
    }
}

这里写图片描述

  1. 继承
    2.1 结构包装
#ifndef _COBJ2_H
#define _COBJ2_H

#ifdef __cplusplus
extern "C"{
#endif

    typedef void* HSTUDENT;

    HSTUDENT createStudent(const char* name);
    void setStudent(HSTUDENT person, int age, int number,int score);
    void displayStudent(HSTUDENT person);
    void deleteStudent(HSTUDENT person);

#ifdef __cplusplus
}
#endif

#endif

#define _CRT_SECURE_NO_WARNINGS
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include "cobj2.h"
#include "cobj1.h"

typedef struct _Student{
    Person person;
    int score;
}Student;
HSTUDENT createStudent(const char* name){
    printf("createStudent.\n");
    Student* person = NULL;
    person = (Student*)malloc(sizeof(Student));
    person->person.name = (char*)malloc(strlen(name) + 1);
    strcpy(person->person.name, name);
    person->score = 0;
    return (HSTUDENT)person;
}
void setStudent(HSTUDENT person, int age, int number,int score){
    Student* p = (Student*)person;
    printf("setStudent.\n");
    if (p != NULL){
        setPerson(&p->person, age, number);
        p->score = score;
    }
}
void displayStudent(HSTUDENT person){
    Student* p = (Student*)person;
    printf("\t<<<displayStudent.>>>\n");
    if (p != NULL){
        displayPerson(&p->person);
        printf("score:%d\n",p->score);
    }
}
void deleteStudent(HSTUDENT person){
    Student* p = (Student*)person;
    printf("deleteStudent.\n");
    if (p != NULL){
        free(p->person.name);
        free(p);
    }
}

这里写图片描述
2.2 指针

#ifndef _COBJ3_H
#define _COBJ3_H

#ifdef __cplusplus
extern "C"{
#endif

    typedef void* HPERSON;
    typedef struct _Person{
        char* name;
        int age;
        int number;
        void* priv;
    }Person;
    HPERSON createPerson(const char* name);
    void setPerson(HPERSON person, int age, int number);
    void displayPerson(HPERSON person);
    void deletePerson(HPERSON person);

#ifdef __cplusplus
}
#endif

#endif

#define _CRT_SECURE_NO_WARNINGS
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include "cobj3.h"


HPERSON createPerson(const char* name){
    printf("createPerson.\n");
    Person* person = NULL;
    person = (Person*)malloc(sizeof(Person));
    person->name = (char*)malloc(strlen(name) + 1);
    strcpy(person->name, name);
    person->age = 0;
    person->number = 0;
    return (HPERSON)person;
}
void setPerson(HPERSON person, int age, int number){
    Person* p = (Person*)person; 
    printf("setPerson.\n");
    if (p != NULL){
        p->age = age;
        p->number = number;
    }
}
void displayPerson(HPERSON person){
    Person* p = (Person*)person;
    printf("\t<<<displayPerson.>>>\n");
    if (p != NULL){
        printf("Name:%s\t Number:%d\t Age:%d\n",
            p->name, p->age,p->number);
    }
}
void deletePerson(HPERSON person){
    Person* p = (Person*)person;
    printf("deletePerson.\n");
    if (p != NULL){
        free(p->name);
        free(p);
    }
}

#ifndef _COBJ4_H
#define _COBJ4_H

#ifdef __cplusplus
extern "C"{
#endif

    typedef void* HSTUDENT;

    HSTUDENT createStudent(const char* name);
    void setStudent(HSTUDENT person, int age, int number,int score);
    void displayStudent(HSTUDENT person);
    void deleteStudent(HSTUDENT person);

#ifdef __cplusplus
}
#endif

#endif

#define _CRT_SECURE_NO_WARNINGS
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include "cobj3.h"
#include "cobj4.h"

typedef struct _StudentPriv{    
    int score;
}StudentPriv;
HSTUDENT createStudent(const char* name){
    printf("createStudent.\n");
    Person* person = NULL;
    person = (Person*)malloc(sizeof(Person));
    person->name = (char*)malloc(strlen(name) + 1);
    strcpy(person->name, name);
    person->priv = (StudentPriv*)malloc(sizeof(StudentPriv));
    ((StudentPriv*)person->priv)->score = 0;
    return (HSTUDENT)person;
}
void setStudent(HSTUDENT person, int age, int number,int score){
    Person* p = (Person*)person;
    printf("setStudent.\n");
    if (p != NULL){
        setPerson(p, age, number);
        ((StudentPriv*)p->priv)->score = score;
    }
}
void displayStudent(HSTUDENT person){
    Person* p = (Person*)person;
    printf("\t<<<displayStudent.>>>\n");
    if (p != NULL){
        displayPerson(p);
        printf("score:%d\n", ((StudentPriv*)p->priv)->score);
    }
}
void deleteStudent(HSTUDENT person){
    Person* p = (Person*)person;
    printf("deleteStudent.\n");
    if (p != NULL){
        free(p->name);
        free(p->priv);
        free(p);
    }
}

这里写图片描述

  1. 多态
#ifndef _COBJ5_H
#define _COBJ5_H

#ifdef __cplusplus
extern "C"{
#endif

    typedef void* HPERSON;  
    HPERSON createPerson(const char* name,int age,int number);
    void deletePerson(HPERSON person);

    typedef void* HSTUDENT;
    HSTUDENT createStudent(const char* name, int age, int number, int score);
    void deleteStudent(HSTUDENT person);

    void displayPerson(HPERSON person);
#ifdef __cplusplus
}
#endif

#endif

#define _CRT_SECURE_NO_WARNINGS
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include "cobj5.h"

typedef struct _PersonOps{
    void(*display)(HPERSON person);
}PersonOps;

typedef struct _Person{
    PersonOps* ops;
    char* name;
    int age;
    int number;
}Person;

typedef struct _Student{
    Person person;
    int score;
}Student;

static void do_displayPerson(HPERSON person){
    Person* p = (Person*)person;
    printf("\t<<<displayPerson.>>>\n");
    if (p != NULL){
        printf("Name:%s\t Number:%d\t Age:%d\n",
            p->name, p->age, p->number);
    }
}

static PersonOps ops_person = {
    do_displayPerson
};

static void do_displayStudent(HSTUDENT person){
    Student* p = (Student*)person;
    printf("\t<<<displayStudent.>>>\n");
    if (p != NULL){
        do_displayPerson(person);
        printf("score:%d\n", p->score);
    }
}

static PersonOps ops_student = {
    do_displayStudent
};


HPERSON createPerson(const char* name,int age,int number){
    printf("createPerson.\n");
    Person* person = NULL;
    person = (Person*)malloc(sizeof(Person));
    person->name = (char*)malloc(strlen(name) + 1);
    strcpy(person->name, name);
    person->age = age;
    person->number = number;
    person->ops = &ops_person;
    return (HPERSON)person;
}

HSTUDENT createStudent(const char* name, int age, int number, int score){
    printf("createStudent.\n");
    Student* person = NULL;
    person = (Student*)malloc(sizeof(Student));
    person->person.name = (char*)malloc(strlen(name) + 1);
    strcpy(person->person.name, name);
    person->score = score;
    person->person.age = age;
    person->person.number = number;
    person->person.ops = &ops_student;
    return (HSTUDENT)person;
}

void deletePerson(HPERSON person){
    Person* p = (Person*)person;
    printf("deletePerson.\n");
    if (p != NULL){
        free(p->name);
        free(p);
    }
}

void deleteStudent(HSTUDENT person){
    Person* p = (Person*)person;
    printf("deleteStudent.\n");
    if (p != NULL){
        free(p->name);
        free(person);
    }
}

void displayPerson(HPERSON person){
    ((Person*)person)->ops->display(person);
}

这里写图片描述

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值