子类继承基类后,若子类中有和基类同名的函数,不管参数是否一致,基类的函数就会被隐藏。需要显示调用函数的范围。同名不同参的函数并不会被子类继承下来和子类本身的函数构成重载。同名函数(不管参数和返回值是否相同)要么被被覆盖,要么被隐藏。
#include<iostream>
#include<string.h>
#include<fstream>
using namespace std;
class CB
{
public:
void f(int){
cout << "CB::f(int)" << endl;
}
void ff(){
cout<<"CB::ff()"<<endl;
}
};
class CD : public CB
{
public:
void f(int){
cout << "CD::f(int)" << endl;
}
void test(){
f(1); //调用CD中的f(int),CB中的f(int)被隐藏了起来
}
};
int main(int argc, char* argv[])
{
class CD c;
c.f(2);
c.test();
c.ff();
return 0;
}
结果:
CD::f(int)
CD::f(int)
CB::ff()
默认调用CD中的f(int).
改进如上代码,调用基类中的f()函数
#include<iostream>
#include<string.h>
#include<fstream>
using namespace std;
class CB
{
public:
void f(int){
cout << "CB::f(int)" << endl;
}
void ff(){
cout<<"CB::ff()"<<endl;
}
};
class CD : public CB
{
public:
void f(int){
cout << "CD::f(int)" << endl;
}
void test(){
CB::f(1);
}
};
int main(int argc, char* argv[])
{
class CD c;
c.f(2);
c.test();
c.ff();
return 0;
}
结果:
CD::f(int)
CB::f(int)
CB::ff()
可见添加调用的基类范围就会调用到基类。
对于和基本同名不同参不同返回值情况基类函数还会被隐藏吗?
#include<iostream>
#include<string.h>
#include<fstream>
using namespace std;
class CB
{
public:
int f(int){
cout << "CB::f(int)" << endl;
}
void ff(){
cout<<"CB::ff()"<<endl;
}
};
class CD : public CB
{
public:
void f(int,int){
cout << "CD::f(int,int)" << endl;
}
void test(){
f(1);
}
};
int main(int argc, char* argv[])
{
class CD c;
c.f(2,1);
c.test();
c.ff();
return 0;
}
编译出错:
error: no matching function for call to ‘CD::f(int)’ test()中调用的f(1)依然在派生类中寻找,基类中的同名函数被隐藏了。
同样加上基类范围就可以了。将test()中的f(1)改为CB::f(1) ,顺利调用基类中的f(int)函数,调用成功。
但若将对同名函数的调用放到基类中呢?
#include<iostream>
#include<string.h>
#include<fstream>
using namespace std;
class CB
{
public:
void f(int,int);
void ff(){
cout<<"CB::ff()"<<endl;
f(1,2);
}
};
class CD : public CB
{
public:
void f(int,int){
cout << "CD::f(int)" << endl;
}
void test(){
f(1,1);
}
};
int main(int argc, char* argv[])
{
class CD c;
c.f(2,2);
c.test();
c.ff();
return 0;
}
void CB::f(int,int){
cout << "CB::f(int,int)" << endl;
}
结果:
CD::f(int)
CD::f(int)
CB::ff()
CB::f(int,int)
由结果分析,基类ff()函数中调用f(int ,int)函数,调用的是基类中的f(int,int)函数,而不是派生类的。可见,同名函数在派生类中调用时,基类中的函数会被隐藏(除非指定范围才能调用成功),若在基类中调用同名函数,默认调用的是调用到基类中的。因为在基类中是看不到派生类的,基类无法调用派生类的函数。
如何做到在基类中调用ff(int ,int)依然调用的是派生类的此函数呢?使用virtual关键字。
#include<iostream>
#include<string.h>
#include<fstream>
using namespace std;
class CB
{
public:
virtual void f(int,int){
cout<<"CB::f(int,int)"<<endl;
}
void ff(){
cout<<"CB::ff()"<<endl;
f(1,2);
}
};
class CD : public CB
{
public:
void f(int,int){
cout << "CD::f(int,int)" << endl;
}
void test(){
f(1,1);
}
};
int main(int argc, char* argv[])
{
class CD c;
c.f(2,2);
c.test();
c.ff();
return 0;
}
结果:
CD::f(int,int)
CD::f(int,int)
CB::ff()
CD::f(int,int)
加上virtual后基类函数就会被子类覆盖。此函数称为虚函数,它在所有子类中都将是虚函数,不需要再做声明。
虚函数有如下限制:
1.只有成员函数可以被声明为虚函数
2.内嵌函数不可以声明为virtual
3.构造器不可以声明为虚函数,但析构器可以