1. /********************************* 
  2. *    C语言基于对象编程实现封装   * 
  3. *                                * 
  4. *2012年04月12日 星期四 17时25分18秒  * 
  5. *********************************/ 
  6.  
  7. #ifndef __MY_OBJECT_H 
  8. #define __MY_OBJECT_H 
  9.  
  10. #include <stdio.h> 
  11. #include <string.h> 
  12. #include <stdlib.h> 
  13.  
  14. typedef struct _Person 
  15.     char *name; 
  16.     int age; 
  17.     int number; 
  18. }Person; 
  19.  
  20.  
  21. #ifdef __cplusplus 
  22. extern "C"
  23. #endif 
  24.  
  25. /*HPERSON 是一个句柄定义,实际类型为 void* */ 
  26.  
  27. typedef void* HPERSON; 
  28.  
  29. /*声明建立*/ 
  30.  
  31. HPERSON createPerson(const char* name); 
  32.  
  33. /*声明创建 */ 
  34.  
  35. void setPerson(HPERSON person , int age , int number); 
  36.  
  37. /*声明显示*/ 
  38.  
  39. void displayPerson(HPERSON person); 
  40.  
  41. /*释放句柄 */ 
  42.  
  43. void deletePerson(HPERSON person); 
  44.  
  45. #ifdef __cplusplus  
  46. #endif 
  47. #endif 

 

 
  
  1. #include "myObject.h" 
  2.  
  3. HPERSON createPerson(const char* name) 
  4.     printf("***createPerson***\n"); 
  5.     Person* person = NULL; 
  6.      
  7.     person = (Person*)malloc(sizeof(Person)); 
  8.      
  9.     person->name = (char *)malloc(sizeof(char)*(strlen(name)+1)); 
  10.     strcpy(person->name , name); 
  11.     person->age = 0; 
  12.     person->number = 0; 
  13.  
  14.     return (void*)person; 
  15.      
  16. void setPerson(HPERSON person , int age , int number) 
  17.     printf("***setPerson***\n"); 
  18.     if(person) 
  19.     { 
  20.         ((Person *)person)->age = age; 
  21.         ((Person *)person)->number = number; 
  22.     } 
  23.      
  24. void displayPerson(HPERSON person) 
  25.     printf("<<<Display Person Information>>>\n"); 
  26.     if(person) 
  27.     { 
  28.         printf("Name :%s\n Age :%d\n Number :%d\n" , ((Person *)person)->name , ((Person *)person)->age , ((Person *)person)->number); 
  29.     } 
  30. void deletePerson(HPERSON person) 
  31.     printf(">>>deletePerson<<<\n"); 
  32.     if(person) 
  33.     { 
  34.         free(((Person *)person)->name); 
  35.         free(person); 
  36.     } 

 

 
  
  1. #ifndef __EXTENDS_OBJECT_H 
  2. #define __EXTENDS_OBJECT_H 
  3.  
  4. #include <stdio.h> 
  5. #include <string.h> 
  6. #include <stdlib.h> 
  7. #include "myObject.h" 
  8.  
  9. typedef struct _Student 
  10.     Person person; 
  11.     int score; 
  12. }Student; 
  13.  
  14. #ifdef __cplusplus 
  15. extern "C"
  16. #endif 
  17.  
  18. /*HPERSON 是一个句柄定义,实际类型为 void* */ 
  19.  
  20. typedef void* HSTUDENT; 
  21.  
  22. /*声明建立*/ 
  23.  
  24. HSTUDENT createStudent(const char* name); 
  25.  
  26. /*声明创建 */ 
  27.  
  28. void setStudent(HSTUDENT student , int age , int number , int score); 
  29.  
  30. /*声明显示*/ 
  31.  
  32. void displayStudent(HSTUDENT student); 
  33.  
  34. /*释放句柄 */ 
  35.  
  36. void deleteStudent(HSTUDENT student); 
  37.  
  38. #ifdef __cplusplus  
  39. #endif 
  40. #endif 

 

 
  
  1. #include <stdio.h> 
  2. #include <string.h> 
  3. #include <stdlib.h> 
  4.  
  5. #include "extendsObject.h" 
  6.  
  7.  
  8. HSTUDENT createStudent(const char* name) 
  9.     printf("***createStudent***\n"); 
  10.     Student* student = NULL ; 
  11.      
  12.     student = (Student *)malloc(sizeof(Student)); 
  13.      
  14.     student->person.name = (char *)malloc(sizeof(char)*(strlen(name)+1)); 
  15.     strcpy(student->person.name , name); 
  16.     student->person.age = 0; 
  17.     student->person.number = 0; 
  18.     student->score = 0; 
  19.  
  20.     return (void*)student; 
  21.      
  22. void setStudent(HSTUDENT student , int age , int number , int score) 
  23.     printf("***setStudent***\n"); 
  24.     if(student) 
  25.     { 
  26.         ((Student *)student)->person.age = age; 
  27.         ((Student *)student)->person.number = number; 
  28.         ((Student *)student)->score = score; 
  29.     } 
  30.      
  31. void displayStudent(HSTUDENT student) 
  32.     printf("<<<Display Student Information>>>\n"); 
  33.     if(student) 
  34.     { 
  35.         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); 
  36.     } 
  37. void deleteStudent(HSTUDENT student) 
  38.     printf(">>>deleteStudent<<<\n"); 
  39.     if(student) 
  40.     { 
  41.         free(((Student *)student)->person.name); 
  42.         free(student); 
  43.     } 

 

 
  
  1. #include "extendsObject.h" 
  2. #include <stdio.h> 
  3.  
  4. int main(int argc,char **argv) 
  5.  
  6.     HSTUDENT student = NULL; 
  7.     student = createStudent("zhang san"); 
  8.     setStudent(student , 20 , 1 , 80); 
  9.     displayStudent(student); 
  10.     deleteStudent(student); 
  11.              
  12.     return 0;