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 ...
随机推荐
- Pod 生命周期和重启策略
Pod 在整个生命周期中被系统定义为各种状态,熟悉 Pod 的各种状态对于理解如何设置 Pod 的调度策略.重启策略是很有必要的. Pod 的状态 状态值 描述 Pending API Server ...
- .NET 生态系统的蜕变之 .NET 6云原生
云原生的英文名是cloud native,native 就是土著的意思,也就是土著对当地的环境是非常适应的,在云的环境和传统的数据中心是非常不同的,云原生就是要用的云的技术来构建应用, 利用云的技术来 ...
- 基于 Istio 的全链路灰度方案探索和实践
作者|曾宇星(宇曾) 审核&校对:曾宇星(宇曾) 编辑&排版:雯燕 背景 微服务软件架构下,业务新功能上线前搭建完整的一套测试系统进行验证是相当费人费时的事,随着所拆分出微服务数量的不 ...
- OSI模型 & TCP/IP模型
分层思想 分层思想:将复杂 的流程分解 为几个功能相对单一 的子过程 整个流程更加清晰 ,复杂问题简单化 更容易发现问题并针对性的解决问题 分层思想在网络中的应用 OSI模型 国际标准化组织(Inte ...
- idea连接数据库时区:Server returns invalid timezone. Go to 'Advanced' tab and set 'serverTimezone' prope
错误界面 IDEA连接mysql,地址,用户名,密码,数据库名,全都配置好了,点测试连接,咔!不成功! 界面是这样的, 翻译过来就是:服务器返回无效时区.进入"高级"选项卡,手动设 ...
- Kafka面试题总结
1.Kafka 都有哪些特点? 高吞吐量.低延迟:kafka每秒可以处理几十万条消息,它的延迟最低只有几毫秒,每个topic可以分多个partition, consumer group 对partit ...
- Python学习周总结(二)
Python-SecondWeek知识汇总 本周学了好多内容,最头痛的地方还是自己的思维逻辑不过关,还是敲的代码比较少,一个员工管理系统,第一天写搞得头大 ,结果第三遍自己突然懂了,个人的努力才是自己 ...
- [luogu7468]愤怒的小N
定义$count(x)$为$x$二进制下1的个数,答案即$\sum_{0\le x<n,count(x)\equiv 1(mod\ 2)}f(x)$ 考虑预处理出$S_{k,i,p}=\sum_ ...
- [atAGC049E]Increment Decrement
由于每一个操作的逆操作都存在,可以看作将$a_{i}$全部变为0的代价 先考虑第一个问题,即对于确定的$a_{i}$如何处理 如果仅能用第2种操作,定义点$i$的代价为以$i$为左端点或以$i-1$为 ...
- 数字逻辑实践3->EDA技术与Verilog设计
本文属于EDA技术概述类文章 1 EDA技术及其发展 概念 EDA(Electronic Design Automation),指的是以计算机为工作平台,以EDA软件工具为开发环境,以PLD期间或者A ...