class a { char *p; public: int m; void print1() { printf("print a\n"); } a() { m=10; printf("a begin\n"); } ~a() { printf("a end\n"); } }; class b:public a { char *p; public: int m; void print1() { printf("print b\n"); } b(){ printf("b begin\n"); m=100; } ~b(){ printf("b end\n"); } }; int main(int argc, char* argv[]) { a *p = new b; ((b*)p)->print1(); p->print1(); b b1; b1.print1(); printf("\n\n%d, %d\n\n", p->m, ((b*)p)->m); return 0; } 输出结果为: a begin b begin print b print a a begin b begin print b 10, 100 b end a end Press any key to continue
|