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++中重载、重定义、重写概念辨析
重载:函数名相同,函数的参数个数.参数类型或参数顺序三者中必须至少有一种不同.函数返回值的类型可以相同,也可以不相同.发生在一个类内部. 重定义:也叫做隐藏.覆盖,子类重新定义父类中有相同名称的非虚函 ...
随机推荐
- jffs2根文件系统制作
http://www.eetop.cn/blog/html/98/510998-20964.html 作者:刘洪涛,华清远见嵌入式学院高级讲师,ARM公司授权ATC讲师. JFFS2是Flash上应用 ...
- Windows 2008 Server搭建Radius服务器的方法
原地址:http://service.tp-link.com.cn/detail_article_1113.html (图拷贝不过来) Windows 2008 Server搭建Radius服务器的方 ...
- UC浏览器调试移动端网站
准备工作: UC浏览器开发版网址 UC浏览器开发者版下载地址 下载adb_tool 步骤: 1.将adb_tool解压,把里面的文件复制到 C:\Windows\SysWOW64 文件夹下面. 2.运 ...
- bootstrap首页案例
<html><head> <meta http-equiv="Content-Type" content="text/html; chars ...
- mout系统流程
mount()的最后一个参数data是传给文件系统解释的,有兴趣可以跟踪一下系统调用mount的流程(以vfat为例): sys_mount()-->do_mount()-->do_add ...
- Discuz论坛post登录C#源码
总结: loginhash formhash 表单参数 seccode 参数最重要 全局 的 获取验证码 判断验证码 到最后提交登录 它都有存在 ,seccode==idhash COOKIE自动维 ...
- 根据IP定位城市
根据IP定位城市:http://www.sucaihuo.com/js/35.html 示例:http://www.sucaihuo.com/jquery/0/35/demo/
- js 离开页面
序言 大家是否经常遇到在关闭网页的时候,会看到一个确定是否离开当前页面的提示框?想一些在线测试系统.信息录入系统等就经常会有这一些提示,避免用户有意或者无意中关掉了页面,导致数据丢失.这里面的实现过程 ...
- 自定义HttpModule,用于未登录用户,不弹出Windows认证窗口,而是跳转回SSO站点
2012年的一篇随笔记录,可以学习到如何自定义HttpModule,而具体里面针对需求开发的代码,可能未必能让大伙了解到什么,可快速扫描而过. using System; using System.W ...
- jquery+json实现分页效果
son作为一种轻量级的数据交换格式,由于其传输数据格式的方便性,今天偶然想将其应用于分页实现,分页做为web开发一个长久的话题,其应用的高效与重要性就不多说了本文主要技术:反射机制,Json数据格式, ...