Output of C++ Program | Set 3
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的更多相关文章
- Output of C++ Program | Set 18
Predict the output of following C++ programs. Question 1 1 #include <iostream> 2 using namespa ...
- Output of C++ Program | Set 17
Predict the output of following C++ programs. Question 1 1 #include <iostream> 2 using namespa ...
- Output of C++ Program | Set 16
Predict the output of following C++ programs. Question 1 1 #include<iostream> 2 using namespac ...
- Output of C++ Program | Set 15
Predict the output of following C++ programs. Question 1 1 #include <iostream> 2 using namespa ...
- Output of C++ Program | Set 14
Predict the output of following C++ program. Difficulty Level: Rookie Question 1 1 #include <iost ...
- Output of C++ Program | Set 13
Predict the output of following C++ program. 1 #include<iostream> 2 using namespace std; 3 4 c ...
- Output of C++ Program | Set 11
Predict the output of following C++ programs. Question 1 1 #include<iostream> 2 using namespac ...
- Output of C++ Program | Set 9
Predict the output of following C++ programs. Question 1 1 template <class S, class T> class P ...
- 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 ...
- Output of C++ Program | Set 6
Predict the output of below C++ programs. Question 1 1 #include<iostream> 2 3 using namespace ...
随机推荐
- (转)刚来的大神彻底干掉了代码中的if else...
一旦代码中 if-else 过多,就会大大的影响其可读性和可维护性. 首先可读性,不言而喻,过多的 if-else 代码和嵌套,会使阅读代码的人很难理解到底是什么意思.尤其是那些没有注释的代码. 其次 ...
- 『学了就忘』Linux基础命令 — 27、搜索操作相关命令
目录 1.whereis命令 2.which命令 3.locate命令 1.whereis命令 whereis是搜索系统命令的命令,也就是说,whereis命令不能搜索普通文件,而只能搜索系统命令. ...
- Socket `accept queue is full ` 但是一个连接需要从SYN->ACCEPT
由于标题长度有限制,我把想要描述的问题再次描述下: 内核通常会为每一个LISTEN状态的Socket维护两个队列: 1 accept队列: listen()函数第二个参数BACKLOG指定,表示已完成 ...
- ucharts tooltip弹窗自定义换行
这个东西吧,也许是因为菜,看了3小时,下面给出解决方案 1. 找到源码下面的这个文件 2. 增加绿色方框中的代码 3.组件调用的时候有一个opts属性 :opts="{ extra: { t ...
- PTA 7-1 还原二叉树 (25分)
PTA 7-1 还原二叉树 (25分) 给定一棵二叉树的先序遍历序列和中序遍历序列,要求计算该二叉树的高度. 输入格式: 输入首先给出正整数N(≤50),为树中结点总数.下面两行先后给出先序和中序遍历 ...
- [python]django rest framework写POST和GET接口
版本声明 python3.6.5 Django-2.0.6 djangorestframework-3.8.2 mysqlclient-1.3.12 1.写一个登录接口,不多说,直接上代码 login ...
- Salesforce Consumer Goods Cloud 浅谈篇一之基础介绍
本篇参考: https://baike.baidu.com/item/%E6%B6%88%E8%B4%B9%E5%93%81/425802?fr=aladdin https://help.salesf ...
- vue如何写组件(script标签引入的方式)
很多人知道.vue结构的单文件组件形式,不过这种单文件组件的结构如果要加入到现有的jquery项目中就比较麻烦了,那如果我们又想用vue来写模板,又不想引入vue-cli管理,那该怎么来写组件呢?别着 ...
- 菜鸡的Java笔记 第七 - java 数组
数组的基本概念 笔试中经常出现的试题或是问题 1.1 概念 数组指的是一组相关变量的集合 如果用最原始的方式定义100个变量的话没问题但是这些变量的关联实在 ...
- 使用PAM模块实现普通用户之间su免密切换
参考自:Allow user1 to "su - user2" without password https://unix.stackexchange.com/questions/ ...