c语言编的程序如何封装,C语言基于对象编程实现封装

C语言基于对象编程实现封装

1.[文件] myObject.h ~ 780B     下载(39)

/*********************************

* C语言基于对象编程实现封装 *

* *

*2012年04月12日 星期四 17时25分18秒 *

*********************************/

#ifndef __MY_OBJECT_H

#define __MY_OBJECT_H

#include

#include

#include

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

2.[文件] myObject.c ~ 881B     下载(30)

#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("<<>>\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<<

if(person)

{

free(((Person *)person)->name);

free(person);

}

}

3.[文件] extendsObject.h ~ 629B     下载(33)

#ifndef __EXTENDS_OBJECT_H

#define __EXTENDS_OBJECT_H

#include

#include

#include

#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

4.[文件] extendsObject.c ~ 1KB     下载(30)

#include

#include

#include

#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("<<>>\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<<

if(student)

{

free(((Student *)student)->person.name);

free(student);

}

}

5.[文件] main.c ~ 250B     下载(32)

#include "extendsObject.h"

#include

int main(int argc,char **argv)

{

HSTUDENT student = NULL;

student = createStudent("zhang san");

setStudent(student , 20 , 1 , 80);

displayStudent(student);

deleteStudent(student);

return 0;

}

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值