1, as long as the class containing pure virtual function is abstract class, cannot instantiate object;
2, about polymorphism, in short, is to use the pointer of the parent class type to its subclass instance, and then invoke the member function of the actual subclass through the pointer of the parent class.
※ Need and virtual function table combined understanding
Routines:
#include <iostream> using namespace std;
Class A//Abstract Classes {public:virtual ~a () {}; virtual void out1 () = 0;
The subclass implements the virtual void Out2 ()//default implementation, and subclasses can override {cout << "A (out2)" << Endl;
} void Out3 ()//enforcing {cout << "A (OUT3)" << Endl;
}
};
Class B:public A {public:b () {};
~b () {};
void Out1 () {cout << "B (OUT1)" << Endl;
} void Out2 () {cout << "B (out2)" << Endl;
} void Out3 () {cout << "B (OUT3)" << Endl;
Private:};
int main () {//a atest;//Compile Error//atest.out1 ();
A *ab = new B;
AB->OUT1 ();
Ab->out2 ();
AB->OUT3 ();
cout << "*************" << Endl;
b *BB = new B;
BB->OUT1 ();
Bb->out2 ();
BB->OUT3 ();
Delete ab;
Delete BB;
System ("pause");
return 0; }
The results of the line: