首先先给出定义吧
重载:同一个类中,函数名相同但是参数不同,不管是否有virtual关键字。
重定义:不同类中(特指基类和子类)函数名相同。但是参数列表和返回值不一定相同。
重写(覆盖):基类函数有virtual关键字,且函数名、参数列表、返回值都相同。属于重定义一种。
名字隐藏:如果对基类的某一成员函数的版本在子类中重定义,那么基类中该函数的版本将被隐藏。
看个小程序。
点击(此处)折叠或打开
- #include <iostream>
- #include <string>
- using namespace std;
-
- class Base {
- public:
- virtual void mf1()
- {
- cout << "Base::virtual void mf1()" << endl;
- }
- virtual void mf1(int) //重载mf1() virtual函数
- {
- cout << "Base::virtual void mf1(int)" << endl;
- }
- int f() const
- {
- cout << "Base::f()\n";
- return 1;
- }
- int f(string) const //重载f()函数
- {
- cout << "Base::f(string)" << endl;
- return 1;
- }
- };
-
- class Derived : public Base {
- public:
- using Base::f; //使用using关键字让f()函数可见
- virtual void mf1() // 重写基类虚函数
- {
- cout << "Derived::virtual void mf1()" << endl;
- }
- int f(int) //重定义f()函数, 可以改变其参数列表
- {
- cout << "Derived::f(int) const\n";
- }
- };
-
-
- class Derived2 : public Base {
- public:
- void f() //重定义f()函数, 可以改变其返回值
- {
- cout << "Derived2::f() const\n";
- }
- };
-
- int main()
- {
- Derived d;
- Derived2 d2;
- int x = 10;
- string str = "hello";
-
-
- d.mf1(); ///调用派生类中的函数
- //! d.mf1(x); ///派生类重写了基类中的mf1虚函数,则基类中其他版本的函数都被隐藏了
- d.f(x); ///调用派生类中的函数
- d.f();
-
- d2.f();
- //! d2.f(str); ///派生类重定义基类中的f函数,则基类中其他版本的函数都被隐藏了
- }