#include<iostream>
using namespace std;
class father
{
public:
virtual void show(){cout<<"call father::show()"<<endl;}
virtual void show(int){cout<<"call father::show(int)"<<endl;}
};
class son:public father
{
public:
void show(){cout<<"call son::show()"<<endl;}
};
int main()
{
father *pf = new father;
father *ps = new son;
father f;
son s;
pf->show();
pf->show(1);
ps->show();
ps->show(1);
cout<<"object call"<<endl<<endl<<endl;
f.show();
f.show(1);
s.show();
//s.show(1); 添加后会出错
}
添加s.show(1)后会出错:classExtend.cpp: 在函数‘int main()’中:
classExtend.cpp:28:10: 错误:对‘son::show(int)’的调用没有匹配的函数
classExtend.cpp:28:10: 附注:备选是:
classExtend.cpp:12:8: 附注:virtual void son::show()
classExtend.cpp:12:8: 附注: 备选需要 0 实参,但提供了 1 个
输出结果:
call father::show()
call father::show(int)call son::show()
call father::show(int)
object call
call father::show()
call father::show(int)
call son::show()