//18. 定义一个基类BaseClass,从它派生出类DerivedClass,BaseClass有成员函数fn1()、fn2(),fn1()是虚函数,DerivedClass也有成员函数fn1()、fn2(),
//在主程序中定义一个DerivedClass的对象,分别用BaseClass和DerivedClass的指针来调用fn1()、fn2()。
#include"iostream"
using namespace std;
class BaseClass
{
public:
virtual void fn1();
void fn2();
BaseClass()
{
cout<<" Destruct BaseClass "<<endl;
}
};
class DerivedClass:public BaseClass
{
public:
void fn1();
void fn2();
DerivedClass()
{
cout<<"Destruct DerivedClass"<<endl;
}
};
int main()
{
DerivedClass d1;
DerivedClass *d2=&d1;
BaseClass *b1=&d1;
d2->fn1();
d2->fn2();
b1->fn1();
b1->fn2();
return 0;
}
void BaseClass::fn1()
{
cout<<"the fn1 of BaseClass"<<endl;
}
void BaseClass::fn2()
{
cout<<"the fn2 of BaseClass"<<endl;
}
void DerivedClass::fn1()
{
cout<<"the fn1 of DerivedClass"<<endl;
}
void DerivedClass::fn2()
{
cout<<"the fn2 of DerivedClass"<<endl;
}