通过一个例子来解释
#include <iostream>
using namespace std;
class Base{
public:
virtual void xfn(int i){
cout << "Base::xfn(int i)" << endl;
}
void yfn(float f){
cout << "Base::yfn(float f)" << endl;
}
void zfn(){
cout << "Base::zfn()" << endl;
}
};
class Derived: public Base{
public:
void xfn(int i){ //函数的覆盖
cout << "Drived::xfn(int i)" << endl;
}
void yfn(int c){ //函数的隐藏
cout << "Drived::yfn(float f)" << endl;
}
void zfn(){ //函数隐藏
cout << "Drived::zfn()" << endl;
}
};
int main(){
Derived d;
Base *pB = &d;
Derived *pD = &d;
pB->xfn(5);
pD->xfn(5);
pB->yfn(3.14);
pD->yfn(3.14);
pB->zfn();
pD->zfn();
return 0;
}
本文通过C++代码实例详细解释了面向对象编程中函数覆盖和函数隐藏的概念,展示了如何在派生类中重写基类函数以及如何隐藏基类的某些成员函数。
2万+

被折叠的 条评论
为什么被折叠?



