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;
}
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

顾文繁

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值