成员函数重载与隐藏

重载隐藏覆盖

重载

  • 一组函数名相同、参数列表不同的函数
  • 处在一个同一个作用域内
  • c++编译器产生符号的机制与函数列表有关

隐藏

当派生类继承基类,基类与派生类有同名函数,那么使用派生类对象调用该函数的时候,基类的函数就被隐藏。

举例
#include <iostream>

using namespace std;

class Base {
    
    
public:
    Base(){
    
    }
    ~Base(){
    
    }
    void look() {
    
     cout << "Base::look()" << endl; }
    void show() {
    
     cout << "Base::show()" << endl; }
    void show(int i) {
    
     cout << "Base::show()" <<":"<< i << endl; }
private:

};

class Drived : public Base
{
    
    
public:
    Drived() {
    
    }
    ~Drived() {
    
    }
    virtual void show() {
    
     cout << "Drived::show()" << endl; }
private:
};
int main()
{
    
    
    Drived d;
    d.show();
    d.Base::show(10);
}

派生类Derived调用show()的时候不会调用基类的show()只会调用派生类的show(),基类的show()被隐藏了。

假如需要派生类对象调用基类函数,那么需要指明作用域

猜你喜欢

转载自blog.csdn.net/weixin_43459437/article/details/143358985