- 封装
#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);
}
}
- 继承
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);
}
}
- 多态
#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);
}