C++面向对象三个概念——重载、覆盖和隐藏

C++面向对象三个概念——重载、覆盖和隐藏

1. overload 重载

  • 同名函数,参数个数或类型不同;
  • 相同作用域,即同一个类。

2. override 覆盖

  • 不在一个作用域,即父类与子类;
  • 子类函数与基类函数同名,参数个数和类型相同;
  • 基类使用virtual关键字,子类使用override关键字。

例外的一个点是协变:基类返回基类指针,子类返回子类指针。此时也是override

#include <iostream>

class Base {
public:
    virtual Base* clone() const {
        std::cout << "Base::clone()" << std::endl;
        return new Base(*this);
    }
};

class Derived : public Base {
public:
    // 使用协变返回类型,返回类型是 Base 的派生类型 Derived*
    Derived* clone() const override {
        std::cout << "Derived::clone()" << std::endl;
        return new Derived(*this);
    }
};

int main() {
    Derived d;
    Base* ptr = &d;
    Base* newPtr = ptr->clone(); // 调用 Derived::clone()
    delete newPtr;
    return 0;
}

3. hide 隐藏

作用域不同,同名函数不能生成override,而是覆盖。子类中的成员函数数据成员,将覆盖基类中的同名成员函数数据成员

如果要调用基类中的同名函数,需要使用基类名::函数名,或者使用using引入。

#include <iostream>
using namespace std;

class Base {
public:
    void func() { cout << "Base::func()" << endl; }
    void func(int) { cout << "Base::func(int)" << endl; }
};

class Derived : public Base {
public:
    void func() { cout << "Derived::func()" << endl; } // 隐藏基类所有 func 版本
    void callBaseFunc() { Base::func(); } // 显式调用基类的 func()
};

int main() {
    Derived d;

    d.func();        // 调用 Derived::func()
    // d.func(1);    // 编译错误,基类 func(int) 被隐藏
    d.Base::func(1); // 显式调用基类的 func(int)

    return 0;
}

4. 引用




    Enjoy Reading This Article?

    Here are some more articles you might like to read next:

  • al-folio 本地部署记录(Ubuntu 24.04)
  • C++ Traits
  • 道格拉斯-普克算法(Douglas–Peucker algorithm)
  • CMake支持库收集
  • QGC代码架构解析:飞行前检查(起飞条件)