C语言实现C++面向对象特性
Posted 顾文繁
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了C语言实现C++面向对象特性相关的知识,希望对你有一定的参考价值。
要点:在结构体当中定义派生类对象指针解决继承问题。
例如定义一个基类:
typedef struct _Person
char* pFName;
char* pLName;
//interface for function
fptrDisplayInfo Display;
fptrWriteToFile WriteToFile;
fptrDelete Delete;
void* pDerivedObj;
Person;
这里的函数均为函数指针:
//declaration of pointers to functions
typedef void (*fptrDisplayInfo)(Person*);
typedef void (*fptrWriteToFile)( Person*, const char*);
typedef void (*fptrDelete)( Person *) ;
这样就能保证通过父类访问到子类的方法,从而使下多态。
在子类中定义结构:
typedef struct _Employee Employee;
typedef struct _Employee
Person* pBaseObj;
char* pDepartment;
char* pCompany;
int nSalary;
//If there is any employee specific functions; add interface here.
Employee;
相应地,在子类中定义父类的结构体指针的方式能够访问到父类的成员变量,实现继承。
通过上述的定义,能够基本实现面向对象的三大特性:封装,继承,多态。
完整代码:
typedef struct _Person Person;
//declaration of pointers to functions
typedef void (*fptrDisplayInfo)(Person*);
typedef void (*fptrWriteToFile)( Person*, const char*);
typedef void (*fptrDelete)( Person *) ;
typedef struct _Person
char* pFName;
char* pLName;
//interface for function
fptrDisplayInfo Display;
fptrWriteToFile WriteToFile;
fptrDelete Delete;
void* pDerivedObj;
Person;
Person* new_Person(const char* pFirstName,
const char* pLastName); //constructor
void delete_Person(Person* const pPersonObj); //destructor
void Person_DisplayInfo(Person* const pPersonObj);
void Person_WriteToFile(Person* const pPersonObj, const char* pFileName);
Person* new_Person(const char* const pFirstName, const char* const pLastName)
Person* pObj = NULL;
//allocating memory
pObj = (Person*)malloc(sizeof(Person));
if (pObj == NULL)
return NULL;
pObj->pFName = (char*)malloc(sizeof(char)*(strlen(pFirstName)+1));
if (pObj->pFName == NULL)
return NULL;
strcpy(pObj->pFName, pFirstName);
pObj->pLName = (char*)malloc(sizeof(char)*(strlen(pLastName)+1));
if (pObj->pLName == NULL)
return NULL;
strcpy(pObj->pLName, pLastName);
//Initializing interface for access to functions
pObj->Delete = delete_Person;
pObj->Display = Person_DisplayInfo;
pObj->WriteToFile = Person_WriteToFile;
return pObj;
void delete_Person(Person* const pPersonObj)
printf("call Person_DisplayInfo()\\n");
free(pPersonObj);
void Person_DisplayInfo(Person* pPersonObj)
printf("call Person_DisplayInfo()\\n");
printf("pFirstName:%s pLastName:%s\\n",pPersonObj->pFName,pPersonObj->pLName);
void Person_WriteToFile(Person* const pPersonObj, const char* pFileName)
printf("call Person_WriteToFile()\\n");
typedef struct _Employee Employee;
typedef struct _Employee
Person* pBaseObj;
char* pDepartment;
char* pCompany;
int nSalary;
//If there is any employee specific functions; add interface here.
Employee;
Person* new_Employee(const char* pFirstName, const char* pLastName,
const char* pDepartment, const char* pCompany,
int nSalary); //constructor
void delete_Employee(Person* pPersonObj); //destructor
void Employee_DisplayInfo(Person* pPersonObj);
void Employee_WriteToFile(Person* const pPersonObj, const char* const pFileName);
//Employee
Person* new_Employee(const char* const pFirstName, const char* const pLastName,
const char* const pDepartment,
const char* const pCompany, int nSalary)
Employee* pEmpObj;
//calling base class construtor
Person* pObj = new_Person(pFirstName, pLastName);
//allocating memory
pEmpObj = (Employee*)malloc(sizeof(Employee));
if (pEmpObj == NULL)
pObj->Delete(pObj);
return NULL;
pEmpObj->pBaseObj = pObj;
pObj->pDerivedObj = pEmpObj;
pEmpObj->pDepartment = (char*)malloc(strlen(pDepartment)+1);
if(pEmpObj->pDepartment == NULL)
return NULL;
strcpy(pEmpObj->pDepartment, pDepartment);
pEmpObj->pCompany = (char*)malloc(strlen(pCompany)+1);
if(pEmpObj->pCompany== NULL)
return NULL;
strcpy(pEmpObj->pCompany, pCompany);
pEmpObj->nSalary = nSalary;
pObj->Delete = delete_Employee;
pObj->Display = Employee_DisplayInfo;
pObj->WriteToFile = Employee_WriteToFile;
return pObj;
#define FREE(memory) if((memory))free((memory));
void delete_Employee(Person* pPersonObj)
printf("=======================delete_Employee============================\\n");
if(!pPersonObj) return;
Employee* employee = (Employee*)pPersonObj->pDerivedObj;
if(employee)
FREE(employee->pCompany);
FREE(employee->pDepartment);
FREE(employee);
delete_Person(pPersonObj);
printf("=======================delete_Employee============================\\n");
void Employee_DisplayInfo(Person* pPersonObj)
printf("=======================Employee_DisplayInfo============================\\n");
Employee* employee = (Employee*)pPersonObj->pDerivedObj;
printf("first name = %s\\n", pPersonObj->pFName);
printf("last name = %s\\n", pPersonObj->pLName);
printf("department name = %s\\n", employee->pDepartment);
printf("company name = %s\\n", employee->pCompany);
printf("salary name = %d\\n", employee->nSalary);
printf("========================Employee_DisplayInfo===========================\\n");
void Employee_WriteToFile(Person* const pPersonObj, const char* const pFileName)
int main()
Person* PersonObj = new_Person("pengdan", "farsight");
Person* EmployeeObj = new_Employee("kouxiaojuan", "liuyan","HR", "TCS", 40000);
PersonObj->Display(PersonObj);
PersonObj->WriteToFile(PersonObj,"persondata.txt");
PersonObj->Delete(PersonObj);
EmployeeObj->Display(EmployeeObj);
EmployeeObj->WriteToFile(EmployeeObj, "employeedata.txt");
EmployeeObj->Delete(EmployeeObj);
return 0;
以上是关于C语言实现C++面向对象特性的主要内容,如果未能解决你的问题,请参考以下文章