重载、隐藏和重写(有的书叫“覆盖”)的区别?

来源:互联网 发布:软件测试流生命周期 编辑:程序博客网 时间:2024/05/21 15:36
C++
1) 重载的特征:
    a) 相同的范围(在同一个类中)
    b) 函数名字必须相同 
    c) 参数不同(指参数类型不同,或参数个数不同,或两者皆有) 
    d) virtual关键字可有可无

例子如下:

[objc] view plaincopy在CODE上查看代码片派生到我的代码片
  1. class Base  
  2. {  
  3. public:  
  4.     void fun(void);  
  5.     int fun(int a);  
  6.     int fun(double b);  
  7.     int fun(int a, int b);  
  8.     int fun(int a, double b);  
  9. };  

上面Base类里fun()属于重载函数,主要函数的重载只是函数参数有关,和函数的返回值无关的。


如下面的例子,只是函数的返回值不同,不属于函数的重载,编译器会报错。

[objc] view plaincopy在CODE上查看代码片派生到我的代码片
  1. class Base  
  2. {  
  3. public:  
  4.     void fun(int a);  
  5.     double fun(int a);  
  6.     int fun(int a);  
  7. };  


2) 隐藏是指派生类的函数屏蔽了与其同名的基类函数。
规则如下:
a)如果派生类的函数与基类的函数同名,但是参数不同。此时,不论有无virtual关键字,   基类的函数将被隐藏(注意别与重载混淆)。 
b)如果派生类的函数与基类的函数同名,并且参数也相同,但是基类函数没有virtual关键字。此时,基类的函数被隐藏(注意别与重写(覆盖)混淆)。

例子:

(a)

[objc] view plaincopy在CODE上查看代码片派生到我的代码片
  1. class Base  
  2. {  
  3. public:  
  4.     // virtual关键字可有可无  
  5.     void fun(int n){   
  6.         cout << "in the Base" << endl;   
  7.     }  
  8. };  
  9.   
  10. class Derivedpublic Base  
  11. {  
  12. public:   
  13.     // 注意参数和基类的是不同的   
  14.     void fun(void){   
  15.         cout << "in the Derived" << endl;  
  16.     }  
  17. };  
  18.   
  19. /* 
  20. 这时候,尽管派生类Derived公共继承于基类Base,但是派生类Derive隐藏基类Base的fun(int n)的函数接口,Derive类的内部只有fun(void)函数。只要派生类Derive的fun()函数参数和基类的不一样(和函数返回值无关),派生类就会隐藏基类的同名函数。 
  21. */  
  22.   
  23. int main(void)  
  24. {  
  25.     Derived test;  
  26.     test.fun(10);// 错误,Derived类没fun(int)这个成员函数  
  27.     test.fun();   // 结果为:"in the Derived"  
  28.     return 0;  
  29. }  


(b)

[objc] view plaincopy在CODE上查看代码片派生到我的代码片
  1. class Base  
  2. {  
  3. public:  
  4.     //注意:没有virtual关键字  
  5.     void fun(int n){  
  6.         cout << "in the Base" << endl;  
  7.     }  
  8. };  
  9.   
  10. class Derivedpublic Base  
  11. {  
  12. public:  
  13.     // 参数和基类相同  
  14.     void fun(int n){  
  15.         cout << "in the Derived" << endl;  
  16.     }  
  17. };  
  18.   
  19. int main(void)  
  20. {  
  21.     Derived test;  
  22.     test.fun(10);    
  23.     /* 
  24.     结果为:"in the Derived" 
  25.     不是基类Base里的"in the Base",因为派生类隐藏了基类同名的函数。 
  26.     */  
  27.     return 0;  
  28. }  

 3) 重写(覆盖)的特征有: 
       a) 不同的范围(分别位于派生类与基类)
       b) 函数名字必须相同
       c) 参数必须相同
       d) 基类函数必须有virtual关键字

例子:
[objc] view plaincopy在CODE上查看代码片派生到我的代码片
  1. #include <iostream.h>   
  2. class Base   
  3. {   
  4. public:   
  5.     void f(int x){ cout << "Base::f(int) " << x << endl; }   
  6.     void f(float x){ cout << "Base::f(float) " << x << endl; }  
  7.     // 必须有virtual关键字  
  8.     virtual void g(void){ cout << "Base::g(void)" << endl;}   
  9. };   
  10. class Derived : public Base   
  11. {   
  12. public:   
  13.     // virtual关键字,可有可无  
  14.     virtual void g(void){ cout << "Derived::g(void)" << endl;}   
  15. };   
  16.   
  17. void main(void)   
  18. {   
  19.     Derived d;   
  20.     Base *pb = &d;   
  21.     pb->f(42);     // 运行结果: Base::f(int) 42   
  22.     pb->f(3.14f);  // 运行结果: Base::f(float) 3.14   
  23.     pb->g();       // 运行结果: Derived::g(void) (动态联编)   
  24. }   


综合例子:

[objc] view plaincopy在CODE上查看代码片派生到我的代码片
  1. #include <iostream.h>   
  2. class Base   
  3. {   
  4. public:   
  5.     virtual void f(float x){ cout << "Base::f(float) " << x << endl; }   
  6.     void g(float x){ cout << "Base::g(float) " << x << endl; }   
  7.     void h(float x){ cout << "Base::h(float) " << x << endl; }   
  8. };   
  9. class Derived : public Base   
  10. {   
  11. public:   
  12.     virtual void f(float x){ cout << "Derived::f(float) " << x << endl; }   
  13.     void g(int x){ cout << "Derived::g(int) " << x << endl; }   
  14.     void h(float x){ cout << "Derived::h(float) " << x << endl; }   
  15. };   
通过分析可得: 
1)函数Derived::f(float)覆盖(重写)了Base::f(float)。 
2)函数Derived::g(int)隐藏了Base::g(float),注意,不是重载。 
3)函数Derived::h(float)隐藏了Base::h(float),而不是覆盖。


看完前面的示例,可能大家还没明白隐藏与覆盖到底有什么区别,因为我们前面都是讲的表面现象,怎样的实现方式,属于什么情况。下面我们就要分析覆盖与隐藏在应用中到底有什么不同之处。在下面的程序中bp和dp指向同一地址,按理说运行结果应该是相同的,可事实并非如此。 


[objc] view plaincopy在CODE上查看代码片派生到我的代码片
  1. void main(void)   
  2. {   
  3.     Derived d;   
  4.     Base *pb = &d;   // Bad : behavior depends on type of the pointer   
  5.     Derived *pd = &d; // Good : behavior depends solely on type of the object  
  6.     // f()为虚函数,动态联编  
  7.     pb->f(3.14f); //运行结果: Derived::f(float) 3.14   
  8.     pd->f(3.14f); //运行结果: Derived::f(float) 3.14   
  9.           
  10.     pb->g(3.14f); //运行结果: Base::g(float) 3.14   
  11.     pd->g(3.14f); //运行结果: Derived::g(int) 3   
  12.     pb->h(3.14f); //运行结果: Base::h(float) 3.14   
  13.     pd->h(3.14f); //运行结果: Derived::h(float) 3.14   
  14. }    


请大家注意,f()函数属于覆盖,而g()与h()属于隐藏。

从上面的运行结果,我们可以注意到在覆盖中,用基类指针和派生类指针调用函数f()时,系统都是执行的派生类函数f(),而非基类的f(),这样实际上就是完成的“接口”功能。

而在隐藏方式中,用基类指针和派生类指针调用函数f()时,系统会进行区分,基类指针调用时,系统执行基类的f(),而派生类指针调用时,系统“隐藏”了基类的f(),执行派生类的f(),这也就是“隐藏”的由来。
0 0
原创粉丝点击