重载 覆盖 隐藏
来源:互联网 发布:运动摄影软件 编辑:程序博客网 时间:2024/06/10 18:55
重载 覆盖 隐藏的区别(函数名都相同)
重载:多个函数都存在,根据参数不同选择调用函数
例如:
//声明(定义在此处省略了)int Add(int a, int b); //函数1int Add(float a, float b); //函数2int ia = 0, ib = 0;float fa = 0.0f, fb = 0.0f;//调用Add(ia, ib); //调用函数1Add(fa, fb); //调用函数2
覆盖:函数名相同、参数相同、有virtual关键字,根据指针所指对象的类型来选择不同类中的函数
例如:
#include <iostream>using namespace std;class class1{public: virtual void Print() { cout << "调用 class1 中的函数" << endl; }};class class2 : public class1{public: void Print() { cout << "调用 class2 中的函数" << endl; }};int main(){ class1* p = new class2; p->Print(); //调用class2中的函数 delete p; p = new class1; p->Print(); //调用class1中的函数 delete p; return 0;}
隐藏:函数名相同、参数不一定相同、没有virtual关键字,根据指针的类型来选择指针类中的函数
例如:
#include <iostream>using namespace std;class class1{public: void Print(int a) { cout << "调用 class1 中的函数" << endl; cout << "输出:" << a << endl; }};class class2 : public class1{public: void Print(float a) { cout << "调用 class2 中的函数" << endl; cout << "输出:" << a << endl; }};int main(){ class1* p = new class2; p->Print(1); //调用class1中的函数 delete p; p = new class1; p->Print(2.0f); //调用class1中的函数, 2.0f被转换为2i delete p; return 0;}
已在VS2013上通过测试
0 0
- 重载、覆盖与隐藏
- 重载、覆盖与隐藏
- 重载、覆盖与隐藏
- 覆盖、重载、隐藏
- c++ 覆盖、重载、隐藏
- 重载,覆盖,隐藏
- 重载、覆盖与隐藏
- 重载、覆盖与隐藏
- 重载 覆盖 隐藏
- c++ 覆盖、重载、隐藏
- 覆盖,隐藏,重载
- 重载、覆盖、隐藏
- 重载 覆盖 隐藏
- c++ 覆盖、重载、隐藏
- c++ 覆盖、重载、隐藏
- 重载 ,覆盖,隐藏 区别
- 隐藏 重载和覆盖
- c++(重载、覆盖、隐藏)
- Java实现验证码发送以及Session缓存
- Eclipse中注释模板设置
- 如何成为云计算领域的专家
- JAVA使用Marvin在图片中搜索图片
- 白屏时间,domc,整页时间,首屏时间
- 重载 覆盖 隐藏
- 读书笔记--原型模式
- 法制在线 康姿百德被诬陷坑骗消费者
- Git 远程仓库
- JQuery坑,说说哪些大家都踩过的坑
- 分别使用c、shell语言实现文件内容合并 -- 2014百度面试题目
- 导入LitJson.dll文件到Unity的Plugin目录下,判断Json对象有无某个key的方法
- KNN算法
- Curator之Master/Leader选举注意事项