U E D R , A S I H C RSS

데블스캠프2012/다섯째날/C로배우는C++원리

Describe 데블스캠프2012/다섯째날/C로배우는C++원리 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;
}
Valid XHTML 1.0! Valid CSS! powered by MoniWiki
last modified 2021-02-07 05:29:15
Processing time 0.0121 sec