Describe 2012//CC++ here
¶
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
typedef struct Person Person;
typedef struct Student Student;
struct Person {
char name[11];
int age;
};
void setName(void* this,const char* name) {
Person* this_person = (Person*)this;
strncpy(this_person->name, name, 11);
}
void setAge(void* this, const int age) {
Person* this_person = (Person*)this;
this_person->age = age;
}
void print(void* this) {
Person* this_person = (Person*)this;
printf("name = %s, age = %d\n", this_person->name, this_person->age);
}
void createPerson(void* this, const char* name, const int age){
// Person* this_person = (Person*)this;
setName(this, name);
setAge(this,age);
}
struct Student{
Person parent;
char studentID[10];
};
void createStudent(void* this, const char* name, const int age, const char* studentID){
Student* this_student = (Student*)this;
createPerson(this, name, age);
strncpy(this_student->studentID, studentID, 9);
}
void printStudent(void* this) {
Student* this_student = (Student*)this;
Person* this_person = (Person*)this;
printf("name = %s, age = %d, studentID = %s\n", this_person->name, this_person->age, this_student->studentID);
}
int main() {
Student *temp = (Student*)malloc(sizeof(Student));
createStudent(temp, "ChungAng", 100,"20121111");
Person *p;
p=(Person*)temp;
print(p);
//printStudent(p);
free(p);
return 0;
}










