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++面向对象程序设计 ...
随机推荐
- 【334】Python Object-Oriented Programming
Reference: Python中self用法详解 __init__ 方法: 私有变量. Reference: [290]Python 函数 class 里面的 function 创建与此一致,只是 ...
- hibernate查询出的实体,set值后,自动更新到数据库
1.问题症状描述 最近在处理一个新需求问题,代码的大致逻辑是获取一个实体对象,调用该对象的set方法设置其中的某些字段,然后把修改后的实体作为参数供其他地方调用,根据返回值来决定是否更新这个 ...
- dsm winscp 获得 root 权限修改上传文件
使用DSM开了ssh只可以用admin登陆scp没有权限上传文件.可以用以下方法. ssh 登陆 dsm sudo -i 取得root权限 修改 /etc/sudoers 文件中 %administ ...
- java日期公共类
package com.mall.common; import java.text.ParseException; import java.text.SimpleDateFormat; import ...
- rook 删不掉的问题
# kubectl get crd -o yamlapiVersion: v1items:- apiVersion: apiextensions.k8s.io/v1beta1 kind: Custo ...
- java基础二(阅读Head First Java记录)
写在前面的话 本部分是在语法基础上的一些内容,比如内部java函数库,继承多态等 “与”和“或”运算符 1.短运算符(&&,||) &&与,必须表达式两边都为 ...
- intellij idea运行Android程序时报错;Unable to locate adb within SDK
环境:intellij idea15 问题:运行Android时报错Throwable:Unable to locate adb within SDK 解决方法:在SDK安装目录的\platfor ...
- linux之badblocks命令
简介 该命令用来检测硬盘坏道.硬盘坏道问题,如忽视,会随着使用而扩大面积,严重损坏硬盘.一般采用检测坏道,进而屏蔽重分区的方式复用硬盘. 语法 badblock(选项)(参数) -b<区块大小& ...
- 信息传递(tarjan)
信息传递 http://uoj.ac/problem/146 有 n 个同学(编号为 1 到n )正在玩一个信息传递的游戏.在游戏里每人都有一个固定的信息传递对象,其中,编号为 i的同学的信息传递对象 ...
- Python打包工具
打包Python应用,使用工具: 1.Linux和Windows下,使用pyinstaller pyinstaller -F -w XXX.py 在当前文件夹下生成两个文件夹:build .dist ...