c语言实现面向对象
来源:互联网 发布:淘宝服务行业 编辑:程序博客网 时间:2024/05/05 23:19
//cobj.h
#ifndef _COBJ_
#define _COBJ_
typedef void* HPERSON;
HPERSON createPserson(const char *name);
void setPserson(HPERSON person,int age,int
number);
void displayPerson(HPERSON person);
void deletePerson(HPERSON person);
#endif
//cobj.c
#include<stdio.h>
#include<string.h>
#include<stdlib.h>
#include "cobj.h"
typedef struct _person
{
char *name;
int age;
int number;
}person;
HPERSON createPerson(const char *name)
{
printf("createPerson\n");
person *person=NULL;
person=(person*)malloc(sizeof(person));
person->name=malloc(strlen(name)+1);
strcpy(person->name,name);
person->age=0;
person->number=0;
return ((void*)person);
}
void setPserson(HPERSON person,int age,int number)
{
printf("setPserson\n");
if(person)
{
((person*)person->age=age;
((person*)person->number=number;
}
}
void displayPserson(HPERSON person)
{
if(person)
{
printf("name:%s\n number:%d\n age:%d\n",
((person *)person)->name,
((person *)person)->number,
((person *)person)->age);
}
}
void deletePerson(HPERSON person)
{
printf("deletePerson\n");
if(person)
{
free((person*)person->name);
free(person);
}
}
//main.c
#include<stdio.h>
#include<stdlib.h>
#include "cobj.h"
int main(void)
{
HPERSON person=createPerson("xiaoming");
setPerson(person,12,11);
displayPerson(person);
deletePerson(person);
return ;
}
句柄本质是一个void*形式的指针,可以在使用是转换成
任意需要的类型指针。在 c语言中可以使用句柄表示一个
对象,在程序的接口中每一个对象至少需要有建立 删除
两个基本函数,然后根据需要实现其他的操作函数。
- c语言实现面向对象
- C语言实现面向对象
- C语言实现面向对象
- C语言实现面向对象
- C语言实现面向对象
- C语言实现面向对象
- C语言实现面向对象
- C语言实现面向对象
- C语言面向对象的实现
- C语言 实现 面向对象编程 OOC
- c语言实现面向对象编程
- C语言实现面向对象编程
- 用C语言实现面向对象编程
- 用C语言实现面向对象
- C语言面向对象的实现---多态性
- 用 C 语言实现面向对象编程
- C语言 实现 面向对象编程 OOC
- 在C语言中实现面向对象
- 位结构体+大小端模式
- NYOJ--63题小猴子的下落
- 51汇编练手
- 类的声明、定义与初始化
- PHP框架中集成Smarty3模板引擎(二) ------ CakePHP 2.0
- c语言实现面向对象
- android传感器
- 国外程序员和国内程序员的不同分类
- C++著名程序库的比较和学习经验
- VOIP MTK 网络电话 节费软件 IP电话
- gccgdbmakefile
- (2012.01.03) 输出程序中的指定位置的行数与该文件的路径及标题。
- 2011年最后一周重要新闻回顾
- 无需密码进入系统