# Include < Stdio. h >
# Ifndef c_class
# DefineC_class struct
# Endif
C_class
{
C_class*A_this;
Void(*Foo) (c_class*A_this );
IntA;
IntB;
};
C_class B
{ // B inherits
C_class B * B _this; // Order is important
Void ( * Foo) (c_class B * BThis ); // Virtual Functions
Int A;
Int B;
Int C;
};
VoidB _f2 (c_class B*BThis)
{
Printf ("It is B _fun \ n");
}
void a_foo (c_class A * athis)
{< br> printf ( " it is. A = % d \ n " , AThis -> ); // or here
// exit (1);
// printf (" pure virtual cannot execute \ n "); // or
}
VoidB _foo (c_class B*BThis)
{
Printf ("It is B. C = % d \ n", BThis->C );
}
Void A_creat ( Struct A * P)
{
P -> Foo = A_foo;
P -> A = 1 ;
P -> B = 2 ;
P -> A_this = P;
}
Void B _creat ( Struct B * P)
{
P -> Foo = B _foo;
P -> A = 11 ;
P -> B = 12 ;
P -> C = 13 ;
P -> B _this = P;
}
IntMain (IntArgc,Char*Argv [])
{
C_class*Ma,;
C_class B*MB, B;
A_creat ( & A ); // Instantiation
B _creat ( & B );
MB = & B;
Ma = & A;
Ma = (C_class * ) MB; // Introduction of polymorphism pointers
Printf ( " % D \ n " , Ma -> A ); // Unfortunately, function variables are not private.
Ma -> Foo (MA ); // Polymorphism
A. Foo ( & A ); // No Polymorphism
B _f2 ( & B ); // Member functions. function pointers are not used because of efficiency issues.
Getchar ();
Return 0 ;
}