C++中重载、覆盖和隐藏
一,多态性
1,(1)声明了基类的指针,该指针指向基类,该指针永远调用自己的成员函数,不管函数是否为虚函数。
(2)声明了派生类的指针,该指针指向该派生类,该指针永远调用自己的成员函数,不管函数是否为虚函数。
(3)声明了基类的指针,该指针指向任意一个子类对象,该指针调用一个函数(此函数在基类存在,而且在子类中被重新定义),此时编译系统不清楚该调用哪一个函数,而出错。我们可以将该函数设定为虚函数,则程序运行时候,编译系统会知道调用的是基类函数还是子类函数,此即多态。注:如果调用的函数不是虚函数,则调用自己类中的成员函数。如果指针被声明为基类,尽管它指向子类对象,仍然是基类指针。
2,子类重定义父类成员函数方法,被称为覆盖或重写。覆盖的函数都是虚函数。
3,满足虚函数条件或覆盖的特征是:
(1)不同的范围(分别位于派生类与基类);
(2)函数名字相同;
(3)参数相同;
(4)基类函数必须有virtual 关键字。
二,成员函数重载
成员函数被重载的特征:
(1)相同的范围(在同一个类中);
(2)函数名字相同;
(3)参数不同;
(4)virtual 关键字可有可无。
三,成员函数隐藏
看如下代码:
Base.h
#include <iostream>
using namespace std; class Base{
public:
void fn(int x){
cout << "Base" << x << endl;
}
void fn1(int x){
cout << "Base1" << x << endl;
}
};
SubClass.h
#include <iostream>
using namespace std; class SubClass :public Base{
public:
void fn(float x){
cout << "SubClass" << x << endl;
}
void fn1(int x){
cout << "SubClass1" << x << endl;
}
};
main.cpp
#include <iostream>
#include "A.h"
#include "B.h"
#include "C.h"
using namespace std; int main()
{
C c();
cout << c.GetData() << endl;
cout << c.A::GetData() << endl;
cout << c.B::GetData() << endl;
cout << c.C::GetData() << endl;
cout << c.doGetData() << endl;
cout << c.A::doGetData() << endl;
cout << c.B::doGetData() << endl;
cout << c.C::doGetData() << endl;
return ;
}
运行结果:
aaarticlea/png;base64,iVBORw0KGgoAAAANSUhEUgAAApkAAAEiCAYAAACySsl5AAAAAXNSR0IArs4c6QAAAARnQU1BAACxjwv8YQUAAAAgY0hSTQAAeiYAAICEAAD6AAAAgOgAAHUwAADqYAA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2u3xlggAEGzMkULAVLBhhggAEGGGCgqgGVTMCqAjOqNaplgAEGGGBgmQbMyRQyhUwGGGCAAQYYYKC4AZVMqIqjMmJd5ohVv+t3BhhggAFzMgVLwZIBBhhggAEGGKhqQCUTsKrAjGqNahlggAEGGFimAXMyhUwhkwEGGGCAAQYYKG5AJROq4qiMWJc5YtXv+p0BBhhgwJxMwVKwZIABBhhggAEGqhpQyQSsKjCjWqNaBhhggAEGlmnAnEwhU8hkgAEGGGCAAQaKG1DJhKo4KiPWZY5Y9bt+Z4ABBhgwJ1OwFCwZYIABBhhggIGqBlQyAasKzKjWqJYBBhhggIFlGjAnU8gUMhlggAEGGGCAgeIGVDKhKo7KiHWZI1b9rt8ZYIABBszJFCwFSwYYYIABBhhgoKoBlUzAqgIzqjWqZYABBhhgYJkGzMkUMoVMBhhggAEGGGCguAGVTKiKozJiXeaIVb/rdwYYYIABczIFS8GSAQYYYIABBhioakAlE7CqwIxqjWoZYIABBhhYpgFzMoVMIZMBBhhggAEGGChuQCUTquKojFiXOWLV7/qdAQYYYMCcTMFSsGSAAQYYYIABBqoaUMkErCowo1qjWgYYYIABBpZpwJxMIVPIZIABBhhggAEGihtQyYSqOCoj1mWOWPW7fmeAAQYYMCdTsBQsGWCAAQYYYICBqgZUMgGrCsyo1qiWAQYYYICBZRowJ1PIFDIZYIABBhhggIHiBlQyoSqOyoh1mSNW/a7fGWCAAQbMyRQsBUsGGGCAAQYYYKCqAZVMwKoCM6o1qmWAAQYYYGCZBszJFDKFTAYYYIABBhhgoLiB0pXM/wdwYgw+pgDlOgAAAABJRU5ErkJggg==" alt="" />
fn()函数不符合虚函数条件,不是虚函数。
从SubClass角度来说,其有void fn(float x)函数,从Base继承了void fn(int x)函数,因此这两个函数在SubClass内应该是重载的,因此主函数中SubClass对象sc,sc.fn(3.1f)应该调用void fn(float x)函数,sc.fn(1)应该调用void fn(int x),但是运行结果显示两个都调用了void fn(float x)函数,这是因为Base中的void fn(int x)函数被隐藏了。
主函数中sc.fn1(2);时候,通过结果知道调用了void fn1(int x)函数,Base中的void fn1(int x)函数被隐藏了。
因此知道符合函数隐藏条件是:
(1)如果派生类的函数与基类的函数同名,但是参数不同。此时,不论有无virtual关键字,基类的函数将被隐藏。注意别与重载混淆。
(2)如果派生类的函数与基类的函数同名,并且参数也相同,但是基类函数没有virtual关键字。此时,基类的函数被隐藏。注意别与覆盖混淆。
C++中重载、覆盖和隐藏的更多相关文章
- C++中的覆盖与隐藏(详细讲解)
C++类中覆盖与隐藏一直是一个容易理解出错的地方,接下来我就详细讲解一下区别在何处 覆盖指的是子类覆盖父类函数(被覆盖),特征是: 1.分别位于子类和父类中 2.函数名字与参数都相同 3.父类的函数是 ...
- JAVA中方法和变量在继承中的覆盖和隐藏
出处:http://renyanwei.iteye.com/blog/258304 我们知道,在JAVA中,子类可以继承父类,如果子类声明的方法与父类有重名的情况怎么办,大伙儿都知道要是重写,但是实际 ...
- JAVA中方法和变量在继承中的覆盖和隐藏(一)
我们知道,在JAVA中,子类可以继承父类,如果子类声明的方法与父类有重名的情况怎么办,大伙儿都知道要是重写,但是实际上这又分为两种情况,就是方法和变量在继承时的覆盖和隐藏问题,这些概念性的东西看似无聊 ...
- makefile中重载与取消隐藏规则示例
学习<跟我一起写Makefile-陈皓>后一直不懂,如何重载或取消隐藏规则 为了博客版面整洁,何为隐藏规则,Makefile基本规则编写等基础支持请自行百度. 需要声明的是:这些知识可能在 ...
- Java中的覆盖和隐藏以及final关键字
Java覆盖和隐藏 (1) 变量只能被隐藏(包括静态和非静态),不能被覆盖: (2) 可以用子类的静态变量隐藏父类的静态变量,也可以用子类的非静态变量隐藏父类的静态变量,也可以用非最终变量(final ...
- c++中 重载 覆盖 隐藏的区别 附加 mutable笔记
成员函数被重载的特征有: 1) 相同的范围(在同一个类中): //2) 函数名字相同: 3) 参数不同: 4) virtual关键字可有可无. 覆盖的特征有: 1) 不同的范围(分别位于派生类与基类) ...
- C++之重载覆盖和隐藏
继承体系下同名成员函数的三种关系 重载 在同一作用域内 函数名相同,参数列表不同(分三种情况:参数个数不同,参数类型不同,参数个数和类型都不同) 返回值类型可以相同也可以不同 重写(覆盖) 在不同作用 ...
- C++中的重载、覆盖、隐藏
前几天面试时被问及C++中的覆盖.隐藏,概念基本答不上来,只答了怎么用指针实现多态,也还有遗漏.最终不欢而散.回来后在网上查找学习了一番,做了这个总结.其中部分文字借用了别人的博客,望不要见怪.引用的 ...
- C++中的重载隐藏覆盖&&JAVA中的重载覆盖&&多态
class 类继承默认是private, struct 默认继承是public C++中的隐藏: 只要派生类中出现和基类一样的函数名,基类中的函数就会被派生类中的函数给隐藏(如果派生类和基类中的函数名 ...
- C++中重载、重写(覆盖)和隐藏的区别实例分析
这篇文章主要介绍了C++中重载.重写(覆盖)和隐藏的区别,是C++面向对象程序设计非常重要的概念,需要的朋友可以参考下 本文实例讲述了C++中重载.重写(覆盖)和隐藏的区别,对于C++面向对象程序设计 ...
随机推荐
- mongodb查询速度慢是什么原因?
mongodb查询速度慢是什么原因? 通过mongodb客户端samus代码研究解决问题 最近有项目需要用到mongodb,于是在网上下载了mongodb的源码,根据示例写了测试代码, ...
- VMTurbo采用红帽企业虚拟化软件
VMTurbo公司正处于虚拟化的开始阶段,并将继续向虚拟世界迈进.该公司已宣布官方支持Red Hat 公司的Enterprise Virtualization 3.1.VMTurbo公司采用Red H ...
- Linux mmap函数简介
一.简介 Linux提供了内存映射函数mmap, 它把文件内容映射到一段内存上(准确说是虚拟内存上), 通过对这段内存的读取和修改, 实现对文件的读取和修改, 先来看一下mmap的函数声明: 头文件: ...
- cocos2dx中使用tolua++使lua调用c++函数
一直想学学cocos2dx中如何使用tolua++工具使得lua脚本调用C++函数,今天就来搞一下,顺便记录下来: 首先,我们打开cocos2dx-2.2.4中projects下的test的VS工程, ...
- UI设计可供性解析:巧用隐藏的设计力提升用户体验
以下内容由Mockplus团队翻译整理,仅供学习交流,Mockplus是更快更简单的原型设计工具. 在实际的Web或App界面设计中,设计师们在学习和实践各种专业知识和技能之外,也会不可避免的遇到到各 ...
- 20155230 2016-2017-2 《Java程序设计》第七周学习总结
20155230 2016-2017-2 <Java程序设计>第6周学习总结 教材学习内容总结 世界时:在1972年引入UTC之前,GMT与UT是相同的 格林威治标准时间(GMT),现已不 ...
- WCF技术剖析(卷1)WCF全面解析文摘
第1章 wcf简介 soa体现的是一种对关注点进行分解的思想,与技术无关 soa的基本思想: a.服务自治 独立部署,不依赖其他 b.依赖于开放的标准 采用xml,xsd,及wsdl作为服务描述的 ...
- jmeter 各种配置修修改(后续增加)
1.修改物理内存 使用jmeter进行压力测试时遇到一段时间后报内存溢出outfmenmory错误,导致jmeter卡死了,先尝试在jmeter.bat中增加了JVM_ARGS="-Xmx ...
- B-spline Curves 学习之B样条曲线的移动控制点、修改节点分析(7)
B-spline Curves: Moving Control Points 本博客转自前人的博客的翻译版本,前几章节是原来博主的翻译内容,但是后续章节博主不在提供翻译,后续章节我在完成相关的翻译学习 ...
- Web Service Error wsse:InvalidSecurity Policy Requires Integrity (Doc ID 1370736.1)
Web Service Error wsse:InvalidSecurity Policy Requires Integrity (Doc ID 1370736.1) Modified: 13 ...