Hello,OOC World![Chap1sec3-X][DRAFT 0.1.0518]
Posted on Tue, 17 May 2011 18:49:05 -1100
#include <stdio.h> struct A { int a; }; void iniA(struct A *THIS){ THIS->a=100; } int main(){ struct A obj; iniA(&obj); printf("the value of obj's a is %d\n",obj.a); return 0; }
#include <stdio.h> struct A { int a; void (*showA)(struct A *); }; static void A_showA(struct A *THIS){ printf("the value of obj’s a is %d\n",THIS->a); } void iniA(struct A *THIS){ THIS->a=100; THIS->showA=A_showA; } int main(){ struct A obj; iniA(&obj); obj.showA(&obj); return 0; }
#include <stdio.h> #include <stdlib.h> struct A { int a; void (*showA)(struct A *); }; static void A_showA(struct A *THIS){ printf("the value of obj’s a is %d\n",THIS->a); } void iniA(struct A *THIS){ THIS->a=100; THIS->showA=A_showA; } struct A * newA(){ struct A * mem=(struct A*)malloc(sizeof(struct A)); iniA(mem); return mem; } int main(){ struct A * obj=newA(); obj->showA(obj); free(obj); return 0; }
#include <stdio.h> #include <stdlib.h> struct A { int a; void (*showA)(struct A *); }; static void A_showA(struct A *THIS){ printf("the value of obj’s a is %d\n",THIS->a); } void iniA(struct A *THIS){ THIS->a=100; THIS->showA=A_showA; } struct A * newA(){ struct A * mem=(struct A*)malloc(sizeof(struct A)); iniA(mem); return mem; } void finA(struct A *THIS){ THIS->a=0; THIS->showA=NULL; } void delA(struct A **THIS){ finA(*THIS); free(*THIS); (*THIS)=NULL; } int main(){ struct A * obj=newA(); obj->showA(obj); delA(&obj); printf("is obj NULL ?\n%s\n",obj==NULL?"True":"False"); return 0; }
#include <stdio.h> #include <stdlib.h> struct A { int a; void (*show)(void *); }; static void A_showA(struct A *THIS){ printf("the value of obj’s a is %d\n",THIS->a); } void iniA(struct A *THIS){ THIS->a=100; THIS->show=(void *)A_showA; } struct B { struct A A; int b; }; static void B_showB(struct B *THIS){ printf("the value of obj’s a is %d\n" "the value of obj’s b is %d\n", THIS->A.a,THIS->b); } void iniB(struct B *THIS){ iniA((struct A*)THIS); THIS->b=200; ((struct A*)THIS)->show=(void *)B_showB; } int main(){ struct B obj; iniB(&obj); struct A *s=&(obj.A); s->show(&obj); return 0; }
#include <stdio.h> #include <stdlib.h> struct A { int a; void (*show)(void *); }; static void A_showA(struct A *THIS){ printf("the value of obj’s a is %d\n",THIS->a); } void iniA(struct A *THIS){ THIS->a=100; THIS->show=(void *)A_showA; } struct B { struct A; int b; }; static void B_showB(struct B *THIS){ printf("the value of obj’s a is %d\n" "the value of obj’s b is %d\n", THIS->a,THIS->b); } void iniB(struct B *THIS){ iniA((struct A*)THIS); THIS->b=200; THIS->show=(void *)B_showB; } int main(){ struct B obj; iniB(&obj); obj.show(&obj); return 0; }
stuct B{ struct A{ int a; }; int b; };
struct A{ int a; }; stuct B{ struct A; int b; };