-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathcImpleClass.c
More file actions
94 lines (76 loc) · 2.07 KB
/
cImpleClass.c
File metadata and controls
94 lines (76 loc) · 2.07 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
#include<stdio.h>
#include <stdlib.h>
#include <string.h>
typedef struct _Person Person;
typedef struct _Employee Employee;
typedef void (*fptrDisplayInfo)(const Person*);
typedef struct _Person{ // base class
void* _pDerivedObj;
char *_a;
char *_b;
fptrDisplayInfo _display; // memerber function
}Person;
typedef struct _Employee{ // derived class
Person* _Person;
double _wage;
fptrDisplayInfo _display;
};
void displayPerson(const Person* n) // member function display in base class Person
{
printf("displayPerson : %s %s\n",n->_a,n->_b);
}
void displayEmployee(const Person* n) // member function override display in derived class Employee
{
//printf("displayEmployee: ");
printf("displayEmployee: %s %s %f\n",((Employee*)(n->_pDerivedObj))->_Person->_a,((Employee*)(n->_pDerivedObj))->_Person->_b,((Employee*)(n->_pDerivedObj))->_wage);
}
Person* new_Person(char pFirstName[], char pLastName[])
{
Person* pObj = NULL;
//allocating memory
pObj = (Person*)malloc(sizeof(Person));
pObj->_pDerivedObj = pObj;
pObj->_a = (char*)malloc(sizeof(char)*(strlen(pFirstName)+1));
strcpy(pObj->_a,pFirstName);
pObj->_b = (char*)malloc(sizeof(char)*(strlen(pLastName)+1));
strcpy(pObj->_b,pLastName);
if (pObj == NULL)
{
return NULL;
}
pObj->_display=displayPerson;
return pObj;
}
Person* new_Employee(char pFirstName[], char pLastName[],double wage)
{
Employee* pEmpObj;
//calling base class construtor
Person* pObj = new_Person(pFirstName, pLastName);
//allocating memory
pEmpObj = (Employee*)malloc(sizeof(Employee));
if (pEmpObj == NULL)
{
return NULL;
}
pObj->_pDerivedObj = pEmpObj; //pointing to derived object
pEmpObj->_wage=wage;
pEmpObj->_Person=pObj;
pObj->_display=displayEmployee;
return pObj;
}
void delete_Person(Person* pObj)
{
if (pObj == NULL)
return ;
delete pObj;
}
int main()
{
Person* per=new_Person("number1","number2");
Person* emp=new_Employee("number3","number4",32.0);
//printf("%d %d\n",p->a,p->b);
per->_display(per);
emp->_display(emp);
//displayPerson(p);
return 0;
}