#include<iostream>
#include<vector>
using namespace std;
class father
{
public:
father()
{
cout << "父亲构造函数" << endl;
}
virtual void funn()
{
cout << "in father class" << endl;
}
~father(){ cout << "父亲析构函数" << endl; }
};
class son :public father
{
public:
son() :father(){ cout << "儿子构造函数" << endl; };
virtual void funn()
{
cout << "in son class" << endl;
}
~son() { cout << "儿子析构函数" << endl; };
};
void test(father& fat)
{
fat.funn();
}
int main()
{
father fat;
son so;
father* fatz;
son* soz;
fatz = &fat;
soz = &so;
fatz = soz; //父类指针指向子类对象
fatz->funn();//使用父类指针调用父类虚函数,实现子类重现效果
}
动态多态!
于 2020-09-04 10:59:41 首次发布