C语言基于对象编程实现封装
2012-09-27 12:58
411 查看
/********************************* * 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; }
本文出自 “Record” 博客,请务必保留此出处http://mjrao.blog.51cto.com/6086668/1008044
相关文章推荐
- .Net网络通讯编程[利用Socket实现字串、文件、序列化对象传输]--使用封装的网络服务1
- .Net网络通讯编程[利用Socket实现字串、文件、序列化对象传输]--使用封装的网络服务2[使用IE浏览本页]
- 大并发 03 基于对象编程与面对对象的编程实现的线程类
- .Net网络通讯编程[利用Socket实现字串、文件、序列化对象传输]--使用封装的网络服务3[聊天室][使用IE浏览本页]
- .Net网络通讯编程[利用Socket实现字串、文件、序列化对象传输]--使用封装的网络服务4[聊天室][使用IE浏览本页]
- 第四章 基于对象的编程风格(如何实现一个class)
- .Net网络通讯编程[利用Socket实现字串、文件、序列化对象传输]--使用封装的网络服务2
- 实现线程的封装, 2种方式(面向对象,基于对象)
- 网络编程:基于C语言的简易代理服务器实现(proxylab)
- .Net网络通讯编程[利用Socket实现字串、文件、序列化对象传输]--使用封装的网络服务3[聊天室]
- .Net网络通讯编程[利用Socket实现字串、文件、序列化对象传输]--使用封装的网络服务4[聊天室]
- 网络编程应用:基于TCP协议【实现对象传输】--练习
- C语言实现面对对象编程
- .Net网络通讯编程[利用Socket实现字串、文件、序列化对象传输]--使用封装的网络服务1[使用IE浏览本页]
- 基于Java语言的安卓编程之九混合方式实现UI设置
- 基于visual c++之windows核心编程代码分析(51)基于匿名管道实现远程控制
- C语言里用结构体和指针函数实现面向对象思想
- JAVA--第七周实验-- 继续上周封装对象的编程练习,完成矩阵的运算。
- 基于Visual C++6.0的DDL编程实现
- linux平台下基于C语言实现遍历文件目录