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. kafka的安装

    kafka是基于java环境的,所以需要先安装java环境 centos:yum install java-11-openjdk ubuntu:apt install default-jdk 默安装默 ...

  2. systemd-nspawn以及container的学习

    container的分类 目前container可以分为两大类,一类是Privileged container,一类是Unprivileged container. Privileged contai ...

  3. Spark记录(二):Spark程序的生命周期

    本文以Spark执行模式中最常见的集群模式为例,详细的描述一下Spark程序的生命周期(YARN作为集群管理器). 1.集群节点初始化 集群刚初始化的时候,或者之前的Spark任务完成之后,此时集群中 ...

  4. 设计模式--策略模式Strategy

    策略模式 算法经常需要被改变==使用S 节省资源(很多if else if-.不会被执行,却会被装载到代码段) 动机 在软件构建过程中,某些对象使用的算法可能多种多样,经常改变,如果将这些算法都编码到 ...

  5. pyinstaller设置图标出现“struct.error: unpack requires a buffer of 16 bytes”

    pyinstaller设置图标出现"struct.error: unpack requires a buffer of 16 bytes" 直接用png图片改后缀名为ico,然后p ...

  6. centos 添加sudo 权限

    https://apple.stackexchange.com/a/82527 visudo user1    ALL=(user2) NOPASSWD: /bin/bash amy  ALL=(AL ...

  7. ajax - error

    ... <!DOCTYPE html> <html> <head> <meta charset="utf-8"> <title ...

  8. vue开发中的一些简单骚操作

    在开发过程中,我们可以定义很多参数,这时需要通过不同的操作来改变不同的参数,这就比较复杂了, 虽然不难,但是代码多了也不好看,这时我们就可以通过简单的操作就行简化: 1.对象使用方括号 let obj ...

  9. Nginx的try_files指令使用实例

    Nginx的配置语法灵活,可控制度非常高.在0.7以后的版本中加入了一个try_files指令,配合命名location,可以部分替代原本常用的rewrite配置方式,提高解析效率. try_file ...

  10. 关于Java内存泄漏的介绍

    翻译自这篇文章 Java一个最显著的优势就是它的内存管理.你只需要简单地创建对象,而Java垃圾收集器会负责内存的分配与释放.不过,事情并没有那么简单,因为在Java应用中时常会出现内存泄漏. 1. ...