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. Laravel/Lumen 分组求和问题 where groupBy sum

    在Laravel中使用分组求和,如果直接使用Laravel各数据库操作方法,应该会得出来如下代码式: DB::table('table_a') ->where('a','=',1) ->g ...

  2. VSCode C/C++ 开发环境配置 详细教程

    本博客已暂停更新,需要请转新博客http://www.whbwiki.com/335.html VsCode是一个轻量级的编辑器,但是配置有点复杂,这里我做了C/C++开发环境配置总结,适用于wind ...

  3. vue修改启动的端口和host

    打开vue项目(dev) dev/config/ 路径修改index.js文件 然后对host和pord修改指定的即可 host: 'localhost', // can be overwritten ...

  4. c#.net 实现短网址的简单方法

    短网址,现在很流行了,本文为大家介绍用c#.net实现短网址的方法,有兴趣的朋友,不妨参考下. 短网址,也被叫做网址缩短.网址压缩选装. 这里我们用一个例子来说明其原理吧,假如您带了一包东西去超市购物 ...

  5. Java 初始化与清理

    用构造器确保初始化 如何自定义构造器(constructor)? 构造器方法的名称与类名相同,并且没有返回值. 需要注意,在定义构方法时,方法名前面不要添加任何的类型说明符,格式:类名(){},构造方 ...

  6. dart系列之:dart类中的泛型

    目录 简介 为什么要用泛型 怎么使用泛型 类型擦除 泛型的继承 泛型方法 总结 简介 熟悉JAVA的朋友可能知道,JAVA在8中引入了泛型的概念.什么是泛型呢?泛型就是一种通用的类型格式,一般用在集合 ...

  7. 菜鸡的Java笔记 第九 - java 接收键盘输入

    package mysterious; import java.util.Scanner; public class lianxi { public static void hhh (){ Scann ...

  8. [tc14634]ExtremeSpanningTrees

    保序回归论文题 要求某一个边集为原图的最小生成树,这等价于非树边比所在环(指树上)的所有边小,最大生成树类似 将这些大小关系的限制看作一张有向图,即若要求$w_{i}\le w_{j}$则连边$(i, ...

  9. [noi110]翘课

    发现加边操作不好处理,因此考虑先加完所有边后删边. 删去一对边x到y,如果两者中有一个不翘课显然没有意义,那么如果都翘课了那么就对他们进行判断,如果无法翘课就继续搜下去. 这样的时间复杂度看上去似乎是 ...

  10. [cf1379F]Chess Strikes Back

    考虑将$(2i-1,2j-1)$和$(2i,2j)$缩为一个点,记作$(i,j)$ 对于每一个点,只能选$(2i-1,2j-1)$或$(2i,2j)$(显然不能都选),而这样恰好为$nm$个,因此必须 ...