@ Sun Xin C ++ note

Source: Internet
Author: User

@ Just get independent blog: http://blog.forgetwind.com, welcome to step on.

1: The premise of type conversion, memory model matching.
2: C ++ polymorphism:
When the C ++ compiler is compiling, it finds that print of Class A is a virtual function, and C ++ will use
The late binding technology depends on the object type (Program, We
The first address of Class B passed) to determine which function to call. This capability is called C ++ polymorphism.
(Determine-normal, or during compilation, and-virtual during runtime)


 # Include  <  Iostream  >  
Using Namespace STD;

Class A {
Public :
A (){}
~ A (){}
Virtual Void Print (){
Cout < " A " < Endl;
}
};

Class B: Public A {
Public :
B (){}
~ B (){}
Virtual Void Print (){
Cout < " B " < Endl;
}
};

Int Main (){
B;
A * A = & B;
A -> Print ();
// Late binding to check whether print exists in Class B. If yes, the print of the derived class is called.
Return 0 ;
}

3: Based on the Principle of inheritance, the this pointer in the base class points to the object of the derived class.
(@ Sun Xin C ++ video 3 _ test program, adjustable to view the running process)

 # Include  <  Iostream  >  
Using Namespace STD;

Class A;
A * PT = NULL;

Class A {
Public :
A (){
PT = This ;
}

Virtual Void Print (){
Cout < " This pointer of the base class A points to the object of. " < Endl;
}
};

Class B: Public A {
Public :
B (){}
Virtual Void Print (){
Cout < " This pointer of the base class A points to the object of B. " < Endl;
}
};
B test; // When the base class constructor is called, PT points to this in.

Int Main (){
PT -> Print ();
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.