- /*********************************
- * C语言基于对象编程实现封装 *
- * *
- *2012年04月12日 星期四 17时25分18秒 *
- *********************************/
- #ifndef __MY_OBJECT_H
- #define __MY_OBJECT_H
- #include <stdio.h>
- #include <string.h>
- #include <stdlib.h>
- typedef struct _Person
- {
- char *name;
- int age;
- int number;
- }Person;
- #ifdef __cplusplus
- extern "C"{
- #endif
- /*HPERSON 是一个句柄定义,实际类型为 void* */
- typedef void* HPERSON;
- /*声明建立*/
- 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
- #include "myObject.h"
- HPERSON createPerson(const char* name)
- {
- printf("***createPerson***\n");
- Person* person = NULL;
- person = (Person*)malloc(sizeof(Person));
- person->name = (char *)malloc(sizeof(char)*(strlen(name)+1));
- strcpy(person->name , name);
- person->age = 0;
- person->number = 0;
- return (void*)person;
- }
- void setPerson(HPERSON person , int age , int number)
- {
- printf("***setPerson***\n");
- if(person)
- {
- ((Person *)person)->age = age;
- ((Person *)person)->number = number;
- }
- }
- void displayPerson(HPERSON person)
- {
- printf("<<<Display Person Information>>>\n");
- if(person)
- {
- printf("Name :%s\n Age :%d\n Number :%d\n" , ((Person *)person)->name , ((Person *)person)->age , ((Person *)person)->number);
- }
- }
- void deletePerson(HPERSON person)
- {
- printf(">>>deletePerson<<<\n");
- if(person)
- {
- free(((Person *)person)->name);
- free(person);
- }
- }
- #ifndef __EXTENDS_OBJECT_H
- #define __EXTENDS_OBJECT_H
- #include <stdio.h>
- #include <string.h>
- #include <stdlib.h>
- #include "myObject.h"
- typedef struct _Student
- {
- Person person;
- int score;
- }Student;
- #ifdef __cplusplus
- extern "C"{
- #endif
- /*HPERSON 是一个句柄定义,实际类型为 void* */
- typedef void* HSTUDENT;
- /*声明建立*/
- HSTUDENT createStudent(const char* name);
- /*声明创建 */
- void setStudent(HSTUDENT student , int age , int number , int score);
- /*声明显示*/
- void displayStudent(HSTUDENT student);
- /*释放句柄 */
- void deleteStudent(HSTUDENT student);
- #ifdef __cplusplus
- }
- #endif
- #endif
- #include <stdio.h>
- #include <string.h>
- #include <stdlib.h>
- #include "extendsObject.h"
- HSTUDENT createStudent(const char* name)
- {
- printf("***createStudent***\n");
- Student* student = NULL ;
- student = (Student *)malloc(sizeof(Student));
- student->person.name = (char *)malloc(sizeof(char)*(strlen(name)+1));
- strcpy(student->person.name , name);
- student->person.age = 0;
- student->person.number = 0;
- student->score = 0;
- return (void*)student;
- }
- void setStudent(HSTUDENT student , int age , int number , int score)
- {
- printf("***setStudent***\n");
- if(student)
- {
- ((Student *)student)->person.age = age;
- ((Student *)student)->person.number = number;
- ((Student *)student)->score = score;
- }
- }
- void displayStudent(HSTUDENT student)
- {
- printf("<<<Display Student Information>>>\n");
- if(student)
- {
- printf("Name :%s\n Age :%d\n Number :%d\n Score : %d\n" , ((Student *)student)->person.name , ((Student *)student)->person.age , ((Student *)student)->person.number , ((Student *)student)->score);
- }
- }
- void deleteStudent(HSTUDENT student)
- {
- printf(">>>deleteStudent<<<\n");
- if(student)
- {
- free(((Student *)student)->person.name);
- free(student);
- }
- }
- #include "extendsObject.h"
- #include <stdio.h>
- int main(int argc,char **argv)
- {
- HSTUDENT student = NULL;
- student = createStudent("zhang san");
- setStudent(student , 20 , 1 , 80);
- displayStudent(student);
- deleteStudent(student);
- return 0;
- }
转载于:https://blog.51cto.com/mjrao/1008044