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

来源:互联网 发布:科密考勤机数据库下载 编辑:程序博客网 时间:2024/05/22 07:57

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

阅读全文
0 0
原创粉丝点击