c++ 重载、重写、重定义(隐藏)
3.重定义 (redefining):
子类重新定义父类中有相同名称的非虚函数 ( 参数列表可以不同 ) 。
4.隐藏
//
// main.cpp
// TestOveride
//
// Created by New_Life on 2017/4/26.
// Copyright © 2017年 chenhuan001. All rights reserved.
// #include <iostream>
#include <stdio.h>
#include <string.h>
using namespace std; class A {
public:
void overload_print(int a) {//
cout << "overload A: a" << endl;
} void overload_print(int a, int b) {// 2,重载1
cout << "overload A: a , b" << endl;
} /* 编译错误,不能出现重载
* Functions that differ only in their return type cannot be overloaded
* overload_print(1) 的时候,谁知道你调用的什么。
int overload_print(int a) {
return 1;
}
*/ /* 编译错误,static 与 virtual 定义冲突
* 虚函数只能出现在no-static中
* 'virtual' can only appear on non-static member functions
static virtual void virtual_print() {
cout << "virtual A: " << endl;
}
*/ void redefine_print() {//
cout << "redefine A: " << endl;
} // -------------- Test hidden ----------------//
void hidden_print() {
cout << "hidden A:" << endl;
} void hidden_print(int a) {
cout << "hidden A: a" << endl;
} private:
virtual void override_print() {//
cout << "override A: " << endl;
} void redefine_print(int a) {//
cout << "redefine priavte A: a" << endl;
}
}; class B : public A {
public:
void override_print() { //重写(覆盖)了4, private -> public,其实可以看出是一个新的。
cout << "override B: " << endl;
} void redefine_print() {//重定义 3
cout << "redefine B: " << endl;
} void redefine_print(int a) {//重定义 5, 说明父类为private的函数也能重定义,也可以看出一个新的。
cout << "redefine B: a" << endl;
} // ------------- Test hidden -----------------//
void hidden_print(int a, int b) {
cout << "hidden B: a, b" << endl;
}
}; int main(int argc, const char * argv[]) {
B b;
b.overload_print(, ); //打印 重载 b.override_print(); //打印 重写 b.redefine_print(); //打印重定义
b.redefine_print(); //打印重定义,private /* 编译错误,因为这两个父类的函数,因为同名被隐藏了。使用b.A::hidden_print(), b.A::hidden_print(1)即可
* Too few arguments to function call, expected 2, have 0; did you mean 'A::hidden_print'?
b.hidden_print();
b.hidden_print(1);
*/
b.hidden_print(, );
return ;
}
c++ 重载、重写、重定义(隐藏)的更多相关文章
- c++ 浅拷贝和深拷贝 指针和引用的区别 malloc(free)和new(delete)的区别 重载重写重定义
4.malloc(free)和new(delete)的区别 malloc()函数: 1.1 malloc的全称是memory allocation,中文叫动态内存分配. 原型:extern void ...
- 重载重写重定义-易混淆概念-C++编译器处理方式
1.函数重载 1)必须在同一个类中进行. 2)子类无法重载父类的函数,父类同名函数将被名称覆盖 3)重载是在编译期间根据参数类型和个数决定函数调用 2.函数重写 1)必须发生于父类与子类之间 2)并且 ...
- C++ 重载 重写 重定义
重写:存在于类的继承,修饰符是virtual,函数的参数个数,顺序,类型,均相同. 重载:函数的参数列表,类型,顺序不相同. 重定义:对父类的函数进行屏蔽,参数列表可以不相同,没有virtual修饰
- C++ 学习笔记 (八)重载 重写 重定义以及名字覆盖
学习C++必定会遇到重载.重写.重定义.概念的东西多也是学习C++蛋疼之处,但是还是得弄懂,学懂了也就不觉得多了. 概念,特点: 重载: 直白点说就是函数名字相同,传参的个数,类型不一样.判断标准在于 ...
- C++重写与重载、重定义
文章引用自:http://blog.163.com/clevertanglei900@126/blog/static/111352259201102441934870/ 重载overload:是函数名 ...
- (转)C++重写、重载和重定义的区别
C++ 重写重载重定义区别 (源自:http://blog.163.com/clevertanglei900@126/blog/static/111352259201102441934870/) 1 ...
- C++重写(覆盖)、重载、重定义、
总结: 重写(覆盖)override 是指派生类函数重写(覆盖)基类函数 不同的范围,分别位于基类和派生类中 函数的名字相同 参数相同 基类函数必须有virtual关键字 重载overload 成员函 ...
- C++ 虚函数及重载、重定义、重写
#include<iostream> usingnamespace std; class BASE { public: BASE()=default; BASE(int publicVal ...
- C++重写(覆盖)、重载、重定义、多态
1 重写(覆盖)override override是重写(覆盖)了一个方法,以实现不同的功能.一般用于子类在继承父类时,重写(覆盖)父类中的方法.函数特征相同,但是具体实现不同. 重写需要注意: 被重 ...
- C++中重载、重定义、重写概念辨析
重载:函数名相同,函数的参数个数.参数类型或参数顺序三者中必须至少有一种不同.函数返回值的类型可以相同,也可以不相同.发生在一个类内部. 重定义:也叫做隐藏.覆盖,子类重新定义父类中有相同名称的非虚函 ...
随机推荐
- R语言中的数据处理包dplyr、tidyr笔记
R语言中的数据处理包dplyr.tidyr笔记 dplyr包是Hadley Wickham的新作,主要用于数据清洗和整理,该包专注dataframe数据格式,从而大幅提高了数据处理速度,并且提供了 ...
- server后台程序的内存使用问题
眼下我开发的一个server后台程序存在这么一个问题,因为我的程序要不断的收发消息,并做统计.统计用的是stl的多重map.在统计中会不断的往map里赛数据. 可是每次统计后我都会调用clear()去 ...
- python三大神器之virtualenv pip, virtualenv, fabric通称为pythoner的三大神器。
python三大神器之virtualenv pip, virtualenv, fabric通称为pythoner的三大神器. virtualenv virtualenv------用来建立一个虚拟 ...
- RTMP流媒体播放过程:握手,建立连接,建立流,播放
本文讲述从打开一个RTMP流媒体到视音频数据开始播放的整个过程. 播放一个流媒体有两个前提步骤: 第一步,建立一个网络连接(NetConnection): 第二步,建立一个网络流(NetStream) ...
- eclipse新建python项Project interpreter not specified
安装好pydev后新建python项目时提示”Project interpreter not specified“的错误,这是因为没有导入python开发环境所致 解决方法如下:1.找到eclipse ...
- C语言 float、double数据在内存中的存储方式
float在内存中占4个字节(32bit),32bit=符号位(1bit)+指数位(8bit)+底数位(23bit) 指数部分 指数位占8bit,可以表示数值的范围是0-(表示0~255一共256个数 ...
- selenium测试(Java)-- 键盘事件(七)
1 package com.test.key; 2 3 import org.openqa.selenium.By; 4 import org.openqa.selenium.Keys; 5 impo ...
- android 编译 app
有些编写的app需要放到android的源码中进行编译.放置的路径packages/apps/ 编译方法,参考 http://blog.csdn.net/luoshengyang/article/de ...
- Spring bean的初始化及销毁
Spring bean的几个属性:scope.init-method.destroy-method.depends-on等. Scope 在Spring容器中是指其创建的Bean对象相对于其他Bean ...
- e660. 用一组像素创建图像
This example demonstrates how to convert a byte array of pixel values that are indices to a color ta ...