Predict the output of below C++ programs.

  Question 1

 1 #include<iostream>
2 using namespace std;
3
4 class P
5 {
6 public:
7 void print()
8 {
9 cout <<" Inside P::";
10 }
11 };
12
13 class Q : public P
14 {
15 public:
16 void print()
17 {
18 cout <<" Inside Q::";
19 }
20 };
21
22 class R: public Q
23 {
24 };
25
26 int main(void)
27 {
28 R r;
29 r.print();
30 return 0;
31 }

  Output: Inside Q::

  The print function is not defined in class R. So it is looked up in the inheritance hierarchy. print() is present in both classes P and Q, which of them should be called?

  The idea is, if there is multilevel inheritance, then function is linearly searched up in the inheritance heirarchy until a matching function is found.

  Question 2

 1 #include<iostream>
2 #include<stdio.h>
3
4 using namespace std;
5
6 class Base
7 {
8 public:
9 Base()
10 {
11 fun(); //note: fun() is virtual
12 }
13 virtual void fun()
14 {
15 cout<<"\nBase Function";
16 }
17 };
18
19 class Derived: public Base
20 {
21 public:
22 Derived()
23 {
24 }
25 virtual void fun()
26 {
27 cout<<"\nDerived Function";
28 }
29 };
30
31 int main()
32 {
33 Base* pBase = new Derived();
34 delete pBase;
35 return 0;
36 }

  Output: Base Function

  When a virtual function is called directly or indirectly from a constructor (including from the mem-initializer for a data member) or from a destructor, and the object to which the call applies is the object under construction or destruction, the function called is the one defined in the constructor or destructor’s own class or in one of its bases, but not a function overriding it in a class derived from the constructor or destructor’s class, or overriding it in one of the other base classes of the most derived object.

  Because of this difference in behavior, it is recommended that object’s virtual function is not invoked while it is being constructed or destroyed.

  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-27  15:05:32

Output of C++ Program | Set 3的更多相关文章

  1. Output of C++ Program | Set 18

    Predict the output of following C++ programs. Question 1 1 #include <iostream> 2 using namespa ...

  2. Output of C++ Program | Set 17

    Predict the output of following C++ programs. Question 1 1 #include <iostream> 2 using namespa ...

  3. Output of C++ Program | Set 16

    Predict the output of following C++ programs. Question 1 1 #include<iostream> 2 using namespac ...

  4. Output of C++ Program | Set 15

    Predict the output of following C++ programs. Question 1 1 #include <iostream> 2 using namespa ...

  5. Output of C++ Program | Set 14

    Predict the output of following C++ program. Difficulty Level: Rookie Question 1 1 #include <iost ...

  6. Output of C++ Program | Set 13

    Predict the output of following C++ program. 1 #include<iostream> 2 using namespace std; 3 4 c ...

  7. Output of C++ Program | Set 11

    Predict the output of following C++ programs. Question 1 1 #include<iostream> 2 using namespac ...

  8. Output of C++ Program | Set 9

    Predict the output of following C++ programs. Question 1 1 template <class S, class T> class P ...

  9. Output of C++ Program | Set 7

    Predict the output of following C++ programs. Question 1 1 class Test1 2 { 3 int y; 4 }; 5 6 class T ...

  10. Output of C++ Program | Set 6

    Predict the output of below C++ programs. Question 1 1 #include<iostream> 2 3 using namespace ...

随机推荐

  1. RabbitMQ的安装及入门使(Windows)

    1.安装Erlang所以在安装rabbitMQ之前,需要先安装Erlang .点击下载Erlang 执行下载下来的Erlang,全部点击"下一步"就行.安装完成设置一下环境变量. ...

  2. Iceberg概述

    背景 随着大数据领域的不断发展, 越来越多的概念被提出并应用到生产中而数据湖概念就是其中之一, 其概念参照阿里云的简介: 数据湖是一个集中式存储库, 可存储任意规模结构化和非结构化数据, 支持大数据和 ...

  3. 该虚拟机似乎正在使用中。如果该虚拟机未在使用,请按“获取所有权(T)”按钮获取它的所有权

    问题 打开虚拟机镜像时报 VMware该虚拟机似乎正在使用中.如果该虚拟机未在使用,请按"获取所有权(T)"按钮获取它的所有权 解决方法 在你安装的镜像文件目录下找到后缀为.vmx ...

  4. 集合概述&集合之List接口

    集合与数组存储概述 集合.数组都是对多个数据进行存储操作的结构,简称Java容器.此时的存储,主要指的是内存层面的存储,不涉及到持久化的存储(.txt,.jpg,.avi,数据库中) 数组存储的特点: ...

  5. Java之JNDI注入

    Java之JNDI注入 About JNDI 0x01 简介 JNDI(Java Naming and Directory Interface)是SUN公司提供的一种标准的Java命名系统接口,JND ...

  6. uni-app路径规划(打开第三方地图实现)

    百度网盘链接:https://pan.baidu.com/s/1-Ys13GFcnKXB1wkJotcwMw 提取码:16gp 把js文件放在common目录下 引入:    import pathP ...

  7. CobaltStrike上线Linux

    为获得最佳的阅读体验,请访问我的个人主页: https://xzajyjs.cn/ 在红蓝对抗中,我们常需要对目标进行长时间的控制,cobaltstrike原生对于上线windows比较轻松友好,但如 ...

  8. 聊一聊声明式接口调用与Nacos的结合使用

    背景 对于公司内部的 API 接口,在引入注册中心之后,免不了会用上服务发现这个东西. 现在比较流行的接口调用方式应该是基于声明式接口的调用,它使得开发变得更加简化和快捷. .NET 在声明式接口调用 ...

  9. 将Linux中文语言修改成英文的具体操作方法及报错解决

    1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 3 ...

  10. Python 流程控制-分支结构详解

    目录 Python 流程控制--分支结构 1.结构分类 顺序结构 分支结构 循环结构 2.分支结构详解 分支结构 定义格式: if 单支结构 if 双分支结构 if 多分支结构 Python 流程控制 ...