#include<iostream> using namespace std; class A { public: void print() { cout<<"This is A"<<endl; } }; class B : public A { public: void print() { cout<<"This is B"<<endl; } }; int main() { //为了在以后便于区分,我这段main()代码叫做main1 A a; B b; a.print(); b.print(); return 0; }
让后是
#include<iostream> using namespace std; class A { public: virtual void print() { cout<<"This is A"<<endl; } }; class B : public A { public: void print() { cout<<"ThisisB"<<endl; } }; int main() { //为了在以后便于区分,我这段main()代码叫做main1 A a; B b; a.print(); b.print(); return 0; }
其中b中的virtual可以省略,但是仍然是虚函数