C++ 中重载、重写(覆盖)和隐藏的区别

一、基本概念:

重载:是指同一可访问区内被声明的几个具有不同参数列(参数的类型,个数,顺序不同)的同名函数,根据参数列表确定调用哪个函数,重载不关心函数返回类型。

class A{
public:
  void test(int i);
  void test(double i);//overload
  void test(int i, double j);//overload
  void test(double i, int j);//overload
  int test(int i);         //错误,非重载。注意重载不关心函数返回类型。
};

重写(覆盖):是指派生类中存在重新定义的函数。其函数名,参数列表,返回值类型,所有都必须同基类中被重写的函数一致。只有函数体不同(花括号内),派生类调用时会调用派生类的重写函数,不会调用被重写函数。重写的基类中被重写的函数必须有virtual修饰。

#include<iostream>

using namespace std;

class Base
{
public:
    virtual void fun(int i){ cout << "Base::fun(int) : " << i << endl;}
};

class Derived : public Base
{
public:
    virtual void fun(int i){ cout << "Derived::fun(int) : " << i << endl;}
};
int main()
{
    Base b;
    Base * pb = new Derived();
    pb->fun(3);//Derived::fun(int)

    system("pause");
    return 0;
}

隐藏:是指派生类的函数屏蔽了与其同名的基类函数,注意只要同名函数,不管参数列表是否相同,基类函数都会被隐藏。

#include "stdafx.h"
#include "iostream"

using namespace std;

class Base
{
public:
    void fun(double ,int ){ cout << "Base::fun(double ,int )" << endl; }
};

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

int main()
{
    Derive pd;
    pd.fun(1);//Derive::fun(int )
    pd.fun(0.01, 1);//error C2660: “Derive::fun”: 函数不接受 2 个参数

    Base *fd = &pd;
    fd->fun(1.0,1);//Base::fun(double ,int);
    fd->fun(1);//error
    system("pause");
    return 0;
}

二、重载和重写的区别

  1. 范围区别:重写和被重写的函数在不同的类中,重载和被重载的函数在同一类中。
  2. 参数区别:重写与被重写的函数参数列表一定相同,重载和被重载的函数参数列表一定不同。
  3. virtual的区别:重写的基类必须要有virtual修饰,重载函数和被重载函数可以被virtual修饰,也可以没有。

三、隐藏与重载,重写的区别

  • 与重载不同的是:范围不同,重载函数与被重载函数在同一区域或者同一个类中,隐藏函数与被隐藏函数不在同一类中(基类与继承类中)。
  • 与重写不同的是:
  1. 当基类函数与继承类函数函数名相同,返回类型,参数列表相同时,基类函数有vitual关键字,此时俩者关系为重写关系,否则为隐藏关系;
  2. 当基类函数函数与继承类函数函数名相同,(返回类型不同的话编译器会直接报错)参数列表不同时,不管基类函数是否有vitual关键字,俩者都为隐藏关系。
 1 #include "stdafx.h"
 2 #include <iostream>
 3 
 4 using namespace std;
 5 
 6 class Base
 7 {
 8 public:
 9     virtual void f(float x){ cout << "Base::f(float) " << x << endl; }
10     void g(float x){ cout << "Base::g(float) " << x << endl; }
11     void h(float x){ cout << "Base::h(float) " << x << endl; }
12 };
13 
14 class Derived : public Base
15 {
16 public:
17     virtual void f(float x){ cout << "Derived::f(float) " << x << endl; }
18     void g(int x){ cout << "Derived::g(int) " << x << endl; }
19     void h(float x){ cout << "Derived::h(float) " << x << endl; }
20 };
21 
22 int main(void)
23 {
24     Derived d;
25     Base *pb = &d;
26     Derived *fd = &d;
27     // Good : behavior depends solely on type of the object
28     pb->f(3.14f); //Derived::f(float) 3.14
29     fd->f(3.14f); //Derived::f(float) 3.14
30 
31     // Bad : behavior depends on type of the pointer
32     pb->g(3.14f); //Base::g(float) 3.14
33     fd->g(3.14f); //Derived::g(int) 3
34 
35     // Bad : behavior depends on type of the pointer
36     pb->h(3.14f); //Base::h(float) 3.14
37     fd->h(3.14f); //Derived::h(float) 3.14
38 
39     system("pause");
40     return 0;
41 }

(1)函数Derived::f(float)覆盖了Base::f(float)。

(2)函数Derived::g(int)隐藏了Base::g(float),而不是重载。

(3)函数Derived::h(float)隐藏了Base::h(float),而不是覆盖。

 

摘自:https://www.cnblogs.com/zhangjxblog/p/8723291.html

 

隐藏:是指派生类的函数屏蔽了与其同名的基类函数,注意只要同名函数,不管参数列表是否相同,基类函数都会被隐藏。

posted @ 2020-06-18 16:30  每天都要吃早饭  阅读(331)  评论(0编辑  收藏  举报