Virtual functions in derived classes
In C++, once a member function is declared as a virtual function in a base class, it becomes virtual in every class derived from that base class. In other words, it is not necessary to use the keyword virtual in the derived class while declaring redefined versions of the virtual base class function.
Source: http://www.umsl.edu/~subramaniana/virtual2.html
For example, the following program prints “C::fun() called” as B::fun() becomes virtual automatically.
1 #include<iostream>
2
3 using namespace std;
4
5 class A {
6 public:
7 virtual void fun()
8 {
9 cout<<"\n A::fun() called ";
10 }
11 };
12
13 class B: public A
14 {
15 public:
16 void fun()
17 {
18 cout<<"\n B::fun() called ";
19 }
20 };
21
22 class C: public B
23 {
24 public:
25 void fun()
26 {
27 cout<<"\n C::fun() called ";
28 }
29 };
30
31 int main()
32 {
33 C c; // An object of class C
34 B *b = &c; // A pointer of type B* pointing to c
35 b->fun(); // this line prints "C::fun() called"
36 getchar();
37 return 0;
38 }
Output: C::fun() called
Please write comments if you find anything incorrect, or you want to share more information about the topic discussed above.
转载请注明:http://www.cnblogs.com/iloveyouforever/
2013-11-26 20:24:37
Virtual functions in derived classes的更多相关文章
- [C++] OOP - Virtual Functions and Abstract Base Classes
Ordinarily, if we do not use a function, we do not need to supply a definition of the function. Howe ...
- [C++] OOP - Base and Derived Classes
There is a base class at the root of the hierarchy, from which the other class inherit, directly or ...
- How can I protect derived classes from breaking when I change the internal parts of the base class?
How can I protect derived classes from breaking when I change the internal parts of the base class? ...
- Standard C++ Programming: Virtual Functions and Inlining
原文链接:http://www.drdobbs.com/cpp/standard-c-programming-virtual-functions/184403747 By Josée Lajoie a ...
- Effective C++ Item 9 Never call virtual functions during constrution or destruction
Because such calls would never go to a more derived class than that of currently executing construto ...
- [CareerCup] 13.3 Virtual Functions 虚函数
13.3 How do virtual functions work in C++? 这道题问我们虚函数在C++中的工作原理.虚函数的工作机制主要依赖于虚表格vtable,即Virtual Table ...
- 【转载】#330 - Derived Classes Do Not Inherit Constructors
A derived class inherits all of the members of a base class except for its constructors. You must de ...
- [译]GotW #5:Overriding Virtual Functions
虚函数是一个很基本的特性,但是它们偶尔会隐藏在很微妙的地方,然后等着你.如果你能回答下面的问题,那么你已经完全了解了它,你不太能浪费太多时间去调试类似下面的问题. Problem JG Ques ...
- 多重继承下的virtual functions
有如下图所示的继承关系: 有如下代码示例: 在早期的未符合c++标准的的编译器上是会报错的,因为对于clone()函数来说,编译器不知道怎么处理处理.但是时至今日c ...
随机推荐
- 【Java】String、StringBuffer、StringBuilder
java.lang.String类 概述 String:代表字符串.Java 程序中的所有字符串字面值(如 "abc" )都作为此类的实例实现 String声明为final,不可被 ...
- Python推导式详解,带你写出比较精简酷炫的代码
Python推导式详解,带你写出比较精简酷炫的代码 前言 1.推导式分类与用法 1.1 列表推导 1.2 集合推导 1.3 字典推导 1.4 元组推导?不存在的 2.推导式的性能 2.1 列表推导式与 ...
- CentOS8安装VNC-Server,并使用VNC Viewer连接
1.查看系统信息 # 查看red-hat版本信息 cat /etc/redhat-release CentOS Linux release 8.0.1905 (Core) 2.安装VNC Server ...
- jenkins项目发布
目录 一.简介 二.docker打包 一.后端打包 二.前端打包 三.启动容器 四.完整代码 五.发布测试 六.优化方案 七.源码地址: 八.参考 一.简介 1.该章节基于jenkins.Harbor ...
- 大爽Python入门教程 3-3 循环:`for`、`while`
大爽Python入门公开课教案 点击查看教程总目录 for循环 可迭代对象iterable 不同于其他语言. python的for循环只能用于遍历 可迭代对象iterable 的项. 即只支持以下语法 ...
- 导出 doc
... /** * 导出word * @return * @throws Exception */ @JCall public String word() throws Exception{ Stri ...
- Python 函数 参数传递
参数传递 在 python 中,类型属于对象,变量是没有类型的: a=[1,2,3] a="Runoob" 以上代码中,[1,2,3] 是 ...
- Redis | 第一部分:数据结构与对象 上篇《Redis设计与实现》
目录 前言 1. 简单动态字符串 1.1 SDS的定义 1.2 空间预分配与惰性空间释放 1.3 SDS的API 2. 链表 2.1 链表与节点的定义 2.2 链表的API 3. 字典 3.1 哈希表 ...
- 【JVM源码解析】模板解释器解释执行Java字节码指令(上)
本文由HeapDump性能社区首席讲师鸠摩(马智)授权整理发布 第17章-x86-64寄存器 不同的CPU都能够解释的机器语言的体系称为指令集架构(ISA,Instruction Set Archit ...
- pyinstaller进行打包exe文件
百度直接pip安装,报错 下载离线文件报错. 百度了一下:还真好使 Python生成可执行文件主要有三种方法,利用py2exe,pyInstaller或cx_Freeze. 这里选择pyinstall ...