Knowledge:
No matter whether there are any virtual functions in the parent class, you can define the parent class pointer to the subclass instance.
If no virtual function exists in the parent class, the parent class pointer is used to access only the members of the parent class, but not the members of the Child class.
If the virtual function in the parent class is not a pure virtual function and the virtual function is not overwritten in the subclass, when the parent class pointer is used to access the virtual function, it is the same as accessing common functions in the parent class.
If the virtual function in the parent class is not a pure virtual function and the virtual function is overwritten in the subclass, when accessing the virtual function with the parent class pointer, the system accesses the rewritten function in the subclass.
If the virtual function in the parent class is a pure virtual function, the parent class is an abstract class. to instantiate a subclass, you must rewrite the pure virtual function. when you use a parent class pointer to access this pure virtual function, the accessed function is rewritten in the subclass.
Another thing to note is that the Destructor should be declared as a virtual function. In this way, when the delete parent class pointer is used, the virtual function of the instantiated subclass will be called, otherwise, only the destructor of the parent class will be called, and the rest of the Child class will not be released, resulting in Memory leakage.
Summary:
When defining a parent class pointer pointing to a subclass instance, the Child class is instantiated in the memory. Because the Child class inherits the parent class, the Child class in the memory contains all the members of the parent class. however, since the parent class pointer is declared, the pointer cannot access the member of the subclass. only members of the parent class can be accessed. however, you can declare pure virtual functions and define virtual functions in the parent class. when you use a parent class pointer to access a virtual function or pure virtual function, you can access a function that is rewritten in the subclass. of course, for a virtual function, if it is not overwritten in the subclass, the virtual function defined in the parent class is still accessed. it can be seen that virtual functions and pure virtual functions are not just defined in pure virtual functions. They only have declarations, and they make the classes with them an abstract class..
The following content comes from: http://blog.chinaunix.net/uid-20665441-id-305464.html 1. If you direct a base class pointer to a base class Object (derived class Object), you can only access functions defined by the base class through this pointer (Static connections) 2. If a textbook class Pointer Points to a basic class object, you must first perform an explicit Transformation (CAST). This approach is dangerous and does not conform to your habits.ProgramIt also brings difficulties to programmers. (It is generally not defined in this way) 3. If the base class and category class define member functions with the same name, then when the object pointer is used to call the member functionIt should be determined based on the pointer prototype, rather than based on the object type actually pointed to by the pointer. The virtual function aims to "if you direct a base class pointer to a struct class object, then through this pointer, you can only access the member functions defined by the basic class. "This rule is the opposite design. If you expect a new member function of the struct class, you can define it as a virtual function ). Polymorphism is a program that processes basic class objects.CodeContinue to properly process future class objects transparently. Pure virtual functions: Virtual void myfunc () = 0; Pure virtual functions are not allowed to define specific actions. They exist only to be redefined in clock-like clocks.As long as classes with pure virtual functions are abstract classes, they cannot be instantiated.(Only inherited).If an inherited class does not rewrite the pure virtual function in the parent class, it is also an abstract class and cannot be instantiated. Abstract classes cannot be instantiated. However, we can have pointers to abstract classes to facilitate operations on each class. Virtual functions are still derived from virtual functions, and the keyword "virtual" can also be omitted ".
Let's look at an example:
# Include <iostream> using namespace STD; Class A {public: Virtual void Foo () {cout <"A's foo ()" <Endl; Bar ();} virtual void bar () {cout <"A's Bar ()" <Endl ;}}; Class B: Public A {public: void Foo () {cout <"B's foo ()" <Endl; A: Foo ();} void bar () {cout <"B's Bar () "<Endl ;}}; int main () {B bobj; A * aptr = & bobj; aptr-> Foo (); A aobj = * aptr; // convert to Class A Object aobj. foo ();} the output result of aptr-> Foo () is: B's foo () // This is clear, and the polymorphism A's foo () // also understands, execute a: Foo (); B's Bar () // although this function is called: A: Foo (); but the address of bobj is passed in implicitly, so calling bar () again will still call function B, which is related to the virtual function pointer aobj. foo (): A's foo () // This Is Not A pointer. aobj is a completely a object and has no relationship with polymorphism. A's Bar ()