虚函数
#include < iostream >
using namespace std;
class BaseClass
{
public:
virtual void fn1() {
cout << "B1" << endl;
}
void fn2() {
cout << "B2" << endl;
}
};
class DerivedClass :public BaseClass
{
public:
void fn1() {
cout << "D1" << endl;
}
void fn2() {
cout << "D2" << endl;
}
};
int main()
{
DerivedClass D;
DerivedClass* d = &D;
BaseClass* b = &D;
b->fn1();
b->fn2();
d->fn1();
d->fn2();
return 0;
}