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

#include
#include

//class poly {
// static class A {
// int i;

/** a struct describing the format of the class */
struct A;
struct A_class {
void (*ping)(void*);
void (*pong)(void*);
};

/** a struct describing the format of the object */
struct A {
struct A_class* class;
int i;
};

/** allocation and initialization of the class table */
void A_ctor(void*,int);
void A_ping(void*);
void A_pong(void*);
struct A_class A_class_table = {A_ping, A_pong};

// void ping() {
// System.out.printf(“A ping %d\n”, this.i);
// }
void A_ping(void* thisv) {
struct A* this = thisv;
printf(“A ping %d\n”, this->i);
}

// void pong() {
// System.out.printf(“A pong %d\n”, this.i);
// }
void A_pong(void* thisv) {
struct A* this = thisv;
printf(“A pong %d\n”, this->i);
}

// public A(int i) {
// this.i = i;
// }
void A_ctor(void* thisv, int i) {
struct A* this = thisv;
this->i = i;
}

/** allocates object and calls constructor on it */
struct A* newA(int i) {
struct A* a = malloc(sizeof (struct A));
a->class = &A_class_table;
A_ctor(a, i);
return a;
}
// }

//
// static class B extends A {

/** a struct describing the format of the class */
struct B;
struct B_class {
void (*ping)(void*);
void (*pong)(void*);
};

/** a struct describing the format of the object */
struct B {
struct B_class* class;
int i;
int j;
};

/** allocation and initialization of the class table */
void B_ctor(void*,int,int);
void B_ping(void*);
struct B_class B_class_table = {B_ping, A_pong};

// @Override
// void ping() {
// System.out.printf(“B ping %d %d\n”, this.i, this.j);
// }
void B_ping(void* thisv) {
struct B* this = thisv;
printf(“B ping %d %d\n”, this->i, this->j);
}

// public B(int i, int j) {
// super(i);
// this.j = j;
// }
void B_ctor(void* thisv, int i, int j) {
struct B* this = thisv;
A_ctor(this, i);
this->j = j;
}

/** allocates object and calls constructor on it */
struct B* newB(int i, int j) {
struct B* b = malloc(sizeof (struct B));
b->class = &B_class_table;
B_ctor(b, i, j);
return b;
}

// }
//
// // This is where the magic happens …
// // a.ping() and a.pong() are dynamic calls
// // which ping/pong is called depends on the actual type of a
// static void foo(A a) {
// a.ping();
// a.pong();
// }
static void foo (void* av) {
struct A* a = av;
a->class->ping(a);
a->class->pong(a);
}
//
// public static void main (String [] args) {
// foo(new A(10));
// }
//}

int main(int argc, char** argv) {
foo(newA(10));
foo(newB(20,30));
}