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的更多相关文章

  1. [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 ...

  2. [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 ...

  3. 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? ...

  4. Standard C++ Programming: Virtual Functions and Inlining

    原文链接:http://www.drdobbs.com/cpp/standard-c-programming-virtual-functions/184403747 By Josée Lajoie a ...

  5. 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 ...

  6. [CareerCup] 13.3 Virtual Functions 虚函数

    13.3 How do virtual functions work in C++? 这道题问我们虚函数在C++中的工作原理.虚函数的工作机制主要依赖于虚表格vtable,即Virtual Table ...

  7. 【转载】#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 ...

  8. [译]GotW #5:Overriding Virtual Functions

       虚函数是一个很基本的特性,但是它们偶尔会隐藏在很微妙的地方,然后等着你.如果你能回答下面的问题,那么你已经完全了解了它,你不太能浪费太多时间去调试类似下面的问题. Problem JG Ques ...

  9. 多重继承下的virtual functions

    有如下图所示的继承关系: 有如下代码示例:                   在早期的未符合c++标准的的编译器上是会报错的,因为对于clone()函数来说,编译器不知道怎么处理处理.但是时至今日c ...

随机推荐

  1. sklearn之转换器和估计器

    sklearn之转换器和估计器 转换器 估计器(sklearn机器学习算法的实现) 转换器 想一下之前做的特征工程的步骤? 实例化(实例化的是一个转换器类(Transformer)--特征工程的父类) ...

  2. elasitcsearch单机版安装

    1.下载压缩包 https://artifacts.elastic.co/downloads/elasticsearch/elasticsearch-6.6.2.tar.gz 2.解压修改配置文件 c ...

  3. 工具篇:apache-httpClient 和 jdk11-HttpClient的使用

    关注公众号,一起交流,微信搜一搜: 潜行前行 HttpClient (apache) apache HttpClient 是 java项目里 较为常用的组件之一:对接外部服务时,各个商家提供的接口是各 ...

  4. Linux基础四:软件包管理

    四.软件包管理器: 1.概念 红帽有两款软件包管理器,分别是rpm和yum. 1.rpm软件包管理器  ->  用来安装单个包  ->  .rpm文件 红帽的安装包文件,都放在Packag ...

  5. 【大爽python算法】递归算法进化之回溯算法(backtracking)

    作者自我介绍:大爽歌, b站小UP主 , python1对1辅导老师, 时常直播编程,直播时免费回答简单问题. 前置知识: 递归算法(recursion algorithm). 我的递归教程: [教程 ...

  6. Django笔记&教程 5-3 综合使用示例

    Django 自学笔记兼学习教程第5章第3节--综合使用示例 点击查看教程总目录 1 - 生成学号场景 场景描述: 教务管理系统中,学生注册账号,学生选择年级后,生成唯一学号. 细节分析: 学生学号由 ...

  7. vi/vim 常用命令总结

    目录 Linux vi/vim编辑 vim键盘图 vim的三种模式 命令模式.输入模式.输出模式 vim使用实例 vi/vim按键说明 第一部分:一般模式可用的光标移动.复制粘贴.搜索替换等 第二部分 ...

  8. VS2013中using System.Windows.Forms;引用不成功

    命名空间"System"中不存在类型或命名空间名称"Windows" 项目右侧--解决资源管理器---引用---右键--添加引用---在.NET下拉框找---找 ...

  9. [bzoj3670]动物园

    首先计算出s数组,s表示可以重复的前缀等于后缀的个数,显然有s[i]=s[next[i]]+1,因为有且仅有next的next满足这个条件. 然后直接暴力枚举所有next,直到它小于i的一半,这个时间 ...

  10. [luogu4107]兔子和樱花

    有两个贪心:1.自底向上dfs,能删就删,这样显然是正确的,因为它最多只会造成它父亲不能删除:2.对于一个节点,优先删除其代价($c[i]+son[i]$)最大的i删除,一定最优,证明略 1 #inc ...