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