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 ...
随机推荐
- 【linux命令】 磁盘管理
du du是查看硬盘的使用情况,统计文件或目录的空间大小. -a 显示所有目录或文件的大小 -b 以byte为单位,显示目录或文件的大小 -c 显示目录或文件的总和 -k 以KB为单位输出 -m 以M ...
- ansible模块及语法
常用模块详解 模块说明及示例: 1.ping模块ping模块 主要用于判断远程客户端是否在线,用于ping本身服务器,返回值是changed.ping示例 ansible clu -m ping 2. ...
- VMware vSphere中三种磁盘:精简置备/厚置备置零/厚置备延迟置零
VMware磁盘格式分类. 厚置备延迟置零.厚置备置零和精简置备1.厚置备延迟置零(zeroed thick) 以默认的厚格式创建虚拟磁盘.创建过程中为虚拟磁盘分配所需空间.创建时不会擦除物理设备上保 ...
- oracle的 listagg() WITHIN GROUP () 行转列函数的使用
1.使用条件查询 查询部门为20的员工列表 -- 查询部门为20的员工列表 SELECT t.DEPTNO,t.ENAME FROM SCOTT.EMP t where t.DEPTNO ...
- .net C# 释放内存 例子
namespace myCommon{ public class SysVar { [DllImport("kernel32.dll")] public ...
- 菜鸡的Java笔记 - java 常用类库
CommonClassLibrary 常用类库 定时调度 定时调度指的是每到一个时刻,都会自动的产生某些特定的操作形式 con ...
- 菜鸡的Java笔记 第二十七 - java 链表基本概念
链表基本概念 1.链表的基本形式 2.单向链表的完整实现 认识链表 链表= 可变长的对象数组,属于动态对象数组的范畴 链表 ...
- [atARC086F]Shift and Decrement
将$A$操作看作直接除以2(保留小数),最终再将$a_{i}$取整 记$k$表示$A$操作的次数,$p_{i}$表示第$i$次$A$和第$i+1$次$A$之间$B$操作的次数(特别的,$p_{0}$为 ...
- [bzoj1635]最高的牛
初始如果没有限制,很显然每一头牛高度都是h当只有一个限制,让h[a]到h[b]的高度都减1即可容易发现两个限制不会相交(否则必然矛盾),只会包含或相离,因此没有影响,直接差分/线段树即可(注意:1.不 ...
- rocketmq 精华
(ps:)通过本人语雀文档阅读体验更好哦--有目录 介绍 rocket mq 翻译成中文就是火箭消息队列,从名字就可以看出来,它是一个很快的消息队列... rocket mq 是 阿里巴巴研制的后面贡 ...