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;
}