C++父类子类间函数重定义,虚函数重写(覆盖)以及重载

写这篇文章的时候我还是不太确定重定义,重写(覆盖),重载之间的确切的区分方法。下面是我今天的理解以及相关测试代码,以待后观!!
第一:简单的子类重定义父类成员函数(函数三要素:返回类型,函数名,参数):既然是重定义,函数名肯定是相同的,不然就没有必要讨论了。三要素都相同,自然可以成立,现在的问题是返回类型和参数是不是可以随意给,同样能隐藏父类的成员函数?
(1)函数名,返回类型,参数都相同

#include <iostream>
using namespace std;
class CBase
{
public:
    void my(int a){
        cout << "父类" << endl;
    }
};
class CDerivedA : public CBase
{
public:
    void my(int a ){
        cout << "子类" << endl;//第一种情况,函数名,返回类型,参数都相同
    }
};
int main()
{
    CDerivedA ptr;
    ptr.my(5);
    system("pause");
    return 0;
}
   
   
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17
  • 18
  • 19
  • 20
  • 21
  • 22
  • 23

上面结果为调用子类成员函数输出:子类
(2)函数名,返回类型相同,参数不同,

#include <iostream>
using namespace std;
class CBase
{
public:
    void  my(int a,int b){
        cout << "父类" << endl;
    }
};
class CDerivedA : public CBase
{
public:
    void my(int a ){
        cout << "子类" << endl;//参数个数不同
    }
};
int main()
{
    CDerivedA ptr;
    ptr.my(5);//(1)给出一个参数
    ptr.my(5,5);//(2)给出两个参数,此时会报错,函数不接受两个参数。说明这个时候,子类并没有继承到父类void my(int a,int b)函数。
    system("pause");
    return 0;
}
   
   
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17
  • 18
  • 19
  • 20
  • 21
  • 22
  • 23
  • 24

一个参数的时候,调用子类的成员函数,输出为:子类。两个参数的时候报错,说明基类方法已经被隐藏了(重新定义继承函数,原来的函数被隐藏)。
(3)重定义继承方法,应确保与原来的原型完全一致——返回类型协变

#include <iostream>
using namespace std;
class CBase
{
public:
    void  my(int a,int b){
        cout << "父类" << endl;
    }
    virtual void Walk(){ cout << "CBase:Walk" << endl; }
};
class CDerivedA : public CBase
{
public:
    void my(int a ){
        cout << "子类" << endl;
    }
    int  Walk(){ cout << "CDerivedA:Walk" << endl; }//重定义类型与原来的类型不同
};
int main()
{
    CDerivedA ptr;
    ptr.Walk();
    system("pause");
    return 0;
}
   
   
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17
  • 18
  • 19
  • 20
  • 21
  • 22
  • 23
  • 24
  • 25

输出结果报错:重写虚函数返回类型有差异,且不是来自“CBase::Walk”的协变

第二:虚函数重写(覆盖)(返回值类型必须相同)
(1)参数列表相同

#include <iostream>
using namespace std;
class CBase
{
public:
    void  my(int a,int b){
        cout << "父类" << endl;
    }
    virtual void Walk(){ cout << "CBase:Walk" << endl; }
};
class CDerivedA : public CBase
{
public:
    void my(int a ){
        cout << "子类" << endl;
    }
    void Walk(){ cout << "CDerivedA:Walk" << endl; }//重写
};
int main()
{
    CDerivedA ptr;
    ptr.Walk();//输出结果为:CDerivedA:Walk
    system("pause");
    return 0;
}
   
   
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17
  • 18
  • 19
  • 20
  • 21
  • 22
  • 23
  • 24
  • 25

这是最常见的对父类虚函数重写

参数列表不同

#include <iostream>
using namespace std;
class CBase
{
public:
    void  my(int a,int b){
        cout << "父类" << endl;
    }
     virtual void Walk(int a){ cout << "CBase:Walk" << endl; }
};
class CDerivedA : public CBase
{
public:
    void my(int a ){
        cout << "子类" << endl;
    }
    void  Walk(){ cout << "CDerivedA:Walk" << endl; }//重写
};
int main()
{
    CDerivedA ptr;
    ptr.Walk(2);//报错:函数不接受 1 个参数
    ptr.Walk();//输出:CDerivedA:Walk
    system("pause");
    return 0;
}
   
   
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17
  • 18
  • 19
  • 20
  • 21
  • 22
  • 23
  • 24
  • 25
  • 26

对虚函数重新定义,但是由于参数列表不同,很容易理解为函数重载。如果是函数重载,那么子类对象 应该继承了父类被重载的成员函数,ptr.Walk(2);就不应该报错。这说明,在这里还是重定义,父类中同名函数被隐藏

第三:父类重定义函数

#include <iostream>
using namespace std;
class CBase
{
public:
    void  my(int a,int b){
        cout << "父类" << endl;
    }
     virtual void Walk(int a){ cout << "CBase:Walk" << endl; }
     virtual void Walk(){ cout << "CBase::::::Walk" << endl; }
};
class CDerivedA : public CBase
{
public:
    void my(int a ){
        cout << "子类" << endl;
    }
    //void  Walk(){ cout << "CDerivedA:Walk" << endl; }//重写
};
int main()
{
    CDerivedA ptr;
    ptr.Walk(2);//输出:CBase:Walk
    ptr.Walk();//输出:CBase::::::Walk
    system("pause");
    return 0;
}
   
   
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17
  • 18
  • 19
  • 20
  • 21
  • 22
  • 23
  • 24
  • 25
  • 26
  • 27

结论:子类不能重载父类函数。即,重载发生在同一个类里面

结论:
在类继承中,重定义,重载。重定义函数名相同,返回类型必须相同,参数列表可以相同,可以不同;重载,函数名相同,返回类型和参数列表至少一个不同。
只要子类出现与父类同名的函数,子类就不会继承父类同名函数(隐藏)。当该同名函数在父类声明为虚函数时(virtual),称为重写(覆盖),非虚函数时,称为重定义

  • 5
    点赞
  • 12
    收藏
    觉得还不错? 一键收藏
  • 2
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 2
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值