CS计算机代考程序代写 #include

#include
#include
#include

struct Person_class {
char* (*get_name)(void*);
void (*print)(void*);
};

struct Person {
struct Person_class* class;
char* name;
};

struct Person_class Person_class_table;

void* new_Person(char* name) {
struct Person* this = malloc(sizeof(*this));
this->class = &Person_class_table;
this->name = strdup(name);
return this;
}

char* Person_get_name(void* thisv) {
struct Person* this = thisv;
return this->name;
}

void Person_print(void* thisv) {
struct Person* this = thisv;
printf(“Name: %s\n”, this->name);
}

struct Person_class Person_class_table = {
Person_get_name,
Person_print
};

struct Student_class {
char* (*get_name)(void*);
void (*print)(void*);
int max_id_so_far;
};

struct Student {
struct Student_class* class;
char* name;
int id;
};

struct Student_class Student_class_table;

void* new_Student(char* name) {
struct Student* this = malloc(sizeof(*this));
this->class = &Student_class_table;
this->name = strdup(name);
this->id = ++ this->class->max_id_so_far;
return this;
}

void Student_print(void* thisv) {
struct Student* this = thisv;
printf(“Name: %s, id %d\n”, this->class->get_name(this), this->id);
}

struct Student_class Student_class_table = {
Person_get_name,
Student_print,
0
};

// STUDENT
// [A] Got it
// [B] Close, but ran out of time
// [C] Got some parts, but stuggled on others
// [D] Struggling, but making progress
// [E] Lost, but excited to discuss in office hours …

int main(int argc, char** argv) {
struct Person* p0 = new_Person(“Alice”);
struct Person* p1 = new_Student(“Bob”);
p0->class->print(p0);
p1->class->print(p1);
}