C的继承和多态

<参考自 http://www.cnblogs.com/skynet/archive/2010/09/23/1833217.html>

 

person.h

 1 // Base class

 2 

 3 #pragma once

 4 

 5 typedef struct _Person Person;

 6 

 7 typedef void (*fptrDisplayInfo)(const Person *);

 8 typedef void (*fptrWritetoFile)(const Person *, const char *);

 9 typedef void (*fptrDelete)(Person *);

10 

11 typedef struct _Person {

12     char * pFName;

13     char * pLName;

14     void * pDerivedObj; // Derived Object

15 

16     fptrDisplayInfo Display; // public interface, private not need to be defined

17     fptrWritetoFile WriteToFile;

18     fptrDelete Delete;

19 }Person;

20 

21 Person * new_person(const char *pFName, const char *pLName);

22 void delete_person(Person * obj);

23 void Person_DisplayInfo(const Person * obj);

24 void Person_WriteToFile(const Person * obj, const char * pFile);

person.c

 1 #include "person.h"

 2 #include <stdlib.h>

 3 #include <string.h>

 4 #include <stdio.h>

 5 

 6 Person * new_person(const char *pFirstName, const char *pLastName) {

 7     Person * pObj = (Person *)malloc(sizeof(Person));

 8     if (pObj == NULL)

 9         return NULL;

10 

11     pObj->pFName = (char *)malloc(strlen(pFirstName)+1);

12     if (pObj->pFName == NULL)

13         return NULL;

14     strcpy(pObj->pFName, pFirstName);

15 

16     pObj->pLName = (char *)malloc(strlen(pLastName)+1);

17     if (pObj->pLName == NULL)

18         return NULL;

19     strcpy(pObj->pLName, pLastName);

20 

21     pObj->Display = Person_DisplayInfo;

22     pObj->WriteToFile = Person_WriteToFile;

23     pObj->Delete = delete_person;

24     pObj->pDerivedObj = pObj;

25 

26     return pObj;

27 }

28 

29 void delete_person(Person * obj) {

30     if (obj == NULL)

31         return;

32     free(obj->pFName); // Caution

33     free(obj->pLName); // Caution

34     free(obj);

35 }

36 

37 void Person_DisplayInfo(const Person * obj) {

38     printf("First Name: %s, Last Name: %s", obj->pFName, obj->pLName);

39 }

40 

41 void Person_WriteToFile(const Person * obj, const char * pFile) {

42     FILE * fp = fopen(pFile, "wb");

43     if (fp == NULL)

44         return;

45     char name[100];

46     sprintf(name, "First Name: %s, Last Name: %s\n", obj->pFName, obj->pLName);

47     fwrite(name, strlen(name)+1, 1, fp);

48 }

emploee.h

 1 // Implement class

 2 

 3 #pragma once

 4 

 5 #include "person.h"

 6 

 7 typedef struct _Emploee Emploee;

 8 

 9 // here add new function pointer

10 

11 typedef struct _Emploee {

12     char * company;

13     char * department;

14     int salary;

15 }Emploee;

16 

17 Person * new_emploee(const char * pFirstName, const char * pLastName, 

18                 const char * pCompany, const char * department, int salary);

19 void delete_emploee(Person * obj);

20 void Emploee_DisplayInfo(const Person * obj);

21 void Emploee_WriteToFile(const Person * obj, const char * pFile);

emploee.c

 1 #include "emploee.h"

 2 #include <stdlib.h>

 3 #include <string.h>

 4 

 5 Person * new_emploee(const char * pFirstName, const char * pLastName, 

 6     const char * pCompany, const char * pDepartment, int salary) {

 7         Person * pObj = new_person(pFirstName, pLastName);

 8         if (pObj == NULL)

 9             return NULL;

10 

11         Emploee  * emploee = (Emploee *)malloc(sizeof(Emploee));

12         if (emploee == NULL) {

13             delete_person(pObj);

14             return NULL;

15         }

16         pObj->pDerivedObj = emploee;

17 

18         emploee->company = (char *)malloc(sizeof(strlen(pCompany)+1));

19         if (emploee->company == NULL) {

20             delete_person(pObj);

21             return NULL;

22         }

23 

24         emploee->department = (char *)malloc(sizeof(strlen(pDepartment)+1));

25         if (emploee->department == NULL) {

26             delete_person(pObj);

27             delete [] emploee->company;

28             return NULL;

29         }

30 

31         emploee->salary = salary;

32         

33         pObj->Delete = delete_emploee;

34         pObj->Display = Emploee_DisplayInfo;

35         pObj->WriteToFile = Emploee_WriteToFile;

36 

37         return pObj;

38 }

39 

40 void delete_emploee(Person * obj) {

41     if (obj == NULL)

42         return;

43     Emploee * pEmploee = (Emploee*)obj->pDerivedObj;

44     free(pEmploee->company);

45     free(pEmploee->department);

46     free(pEmploee);

47     delete_person(obj);

48 }

49 

50 void Emploee_DisplayInfo(const Person * obj) {

51 

52 }

53 

54 void Emploee_WriteToFile(const Person * obj, const char * pFile) {

55 

56 }

main.c

 1 #define _CRTDBG_MAP_ALLOC

 2 #include <crtdbg.h>

 3 #include "person.h"

 4 #include "emploee.h"

 5 

 6 #define NULL 0

 7 

 8 int main()

 9 {

10     Person * person = new_person("Micheal", "Jackson");

11     Person * emploee = new_emploee("Kobe", "Bryant", "Lakes", "Player", 2000);

12 

13     person->Display(person);

14     person->WriteToFile(person, "123.txt");

15     person->Delete(person);

16     person = NULL;

17 

18     emploee->Display(emploee);

19     emploee->WriteToFile(emploee, "456.txt");

20     emploee->Delete(emploee);

21     _CrtDumpMemoryLeaks();

22     return 0;

23 }

 

你可能感兴趣的:(继承)