C Object-Oriented Programming Column

Source: Internet
Author: User

 

# 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 ;
}

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.