C++ Exceptional 类的重载、重写(覆盖)和隐藏
来源:互联网 发布:淘宝生活网 编辑:程序博客网 时间:2024/06/05 01:34
通过下面这个例子可以很好的理解重载、重写(覆盖)和隐藏的关系:
class Base{public: virtual ~Base() {} virtual void f(int); virtual void f(double); virtual void g(int i = 10);};void Base::f(int){ cout << "Base::f(int)" << endl;}void Base::f(double){ cout << "Base::f(double)" << endl;}void Base::g(int i){ cout << "Base::g(int)" << i << endl;}class Derived : public Base{public: void f(complex<double>); void g(int i = 20);};void Derived::f( complex<double> ){ cout << "Derived::f(complex)" << endl;}void Derived::g(int i){ cout << "Derived::g()" << i << endl;}int main(int argc, char * argv[]){ Base b; Derived d; Base* pb = new Derived; b.f(1.0); d.f(1.0); pb->f(1.0); b.g(); d.g(); pb->g(); delete pb;}
下面分别解释以上程序的输出:
1. b.f(1.0) Base::f(double)
2. d.f(1.0) Derivedf(complex)
3. pb->f(1.0) Base::f(double)
4. b.g() Base::g(int) 10
5. d.g() Derived::g(int) 20
6. pb->g() Derived::g(int) 10
重载: Base中的f(int)
和f(double)
为重载关系,重载为在同一个类中,具有相同的函数名,但函数参数不同。
重写(覆盖): Derived中的g(int i = 20)
重写了基类Base中 g(int i = 10)
,但这里要注意的是,当用一个指向子类的基类指针调用g(int)
时,默认的参数是静态绑定的,也就是基类Base中的 i = 10
是静态绑定的,所以 6 才会输出 Derived::g(int) 10
隐藏: Derived中的f( complex<double> )
隐藏了子类中的同名函数。注意这里并不是重写,因为他们的参数不同。但子类中的函数会隐藏父类中的同名函数。所以 2 3 的输出分别如上。pb->f(1.0)
并不会调用Derived中的f(complex<double>)
函数,因为这两者这件的关系并不是重写,而是隐藏,所以调用的仍然是Base中的函数。
1 0
- C++ Exceptional 类的重载、重写(覆盖)和隐藏
- C++_重载、重写(覆盖)和隐藏的区别:
- C++重载、重写(覆盖)和隐藏
- 重载,重写(覆盖)和隐藏的区别
- C++中重载、重写(覆盖)和隐藏的区别
- C++中重载、重写(覆盖)和隐藏的区别
- C++中重载、重写(覆盖)和隐藏的区别
- C++中重载、重写(覆盖)和隐藏的区别
- C++中重载、重写(覆盖)和隐藏的区别
- 重载、重写(覆盖)和隐藏的区别
- C++中重载、重写(覆盖)和隐藏的区别
- C++中重载、重写(覆盖)和隐藏的区别
- 重载、重写(覆盖)、隐藏
- 重载,重写(覆盖),隐藏
- 重载 重写(覆盖) 隐藏
- 重载,重写(覆盖),隐藏
- 重载、覆盖(重写)、隐藏
- 类成员函数的重载、覆盖和隐藏(重写)的区别
- Windows下Eclipse for C/C++的“Launch failed. Binary not found”完美解决方案
- Android基础-TextView用法
- 深搜算法:倒油/面向对象的思想来做
- 123. Best Time to Buy and Sell Stock III
- 快慢指针判断单向链表是否有环及找环入口
- C++ Exceptional 类的重载、重写(覆盖)和隐藏
- [leetcode] 256. Paint House 解题报告
- ef生成codefirst上下版本之间的迁移记录
- Hibernate-Preface(Hibernate前言)
- 实现计时器的几种方式
- [leetcode] 280. Wiggle Sort 解题报告
- 看最强大脑中英PK小记
- python中一些常用的module小结
- [leetcode] 311. Sparse Matrix Multiplication 解题报告