Output of C++ Program | Set 8
Predict the output of following C++ programs.
Question 1
1 #include<iostream>
2 using namespace std;
3
4 class Test1
5 {
6 int x;
7 public:
8 void show()
9 {
10 }
11 };
12
13 class Test2
14 {
15 int x;
16 public:
17 virtual void show()
18 {
19 }
20 };
21
22 int main(void)
23 {
24 cout<<sizeof(Test1)<<endl;
25 cout<<sizeof(Test2)<<endl;
26 return 0;
27 }
Output:
4
8
There is only one difference between Test1 and Test2. show() is non-virtual in Test1, but virtual in Test2. When we make a function virtual, compiler adds an extra pointer vptr to objects of the class. Compiler does this to achieve run time polymorphism (See chapter 15 of Thinking in C++ book for more details). The extra pointer vptr adds to the size of objects, that is why we get 8 as size of Test2.
Question 2
1 #include<iostream>
2 using namespace std;
3 class P
4 {
5 public:
6 virtual void show() = 0;
7 };
8
9 class Q : public P
10 {
11 int x;
12 };
13
14 int main(void)
15 {
16 Q q;
17 return 0;
18 }
Output: Compiler Error
We get the error because we can’t create objects of abstract classes. P is an abstract class as it has a pure virtual method. Class Q also becomes abstract because it is derived from P and it doesn’t implement show().
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:41:28
Output of C++ Program | Set 8的更多相关文章
- 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 ...
随机推荐
- django test, app aren't loaded yet
django.core.exceptions.AppRegistryNotReady: Apps aren't loaded yet. 解决方法: 用django的TestCase from djan ...
- java 邮件 接收与发送
... package com.e6soft; import java.io.BufferedReader; import java.io.FileOutputStream; import java. ...
- bat批处理命令及解释
相关原文链接 一.批处理概念 批处理文件:包含DOS命令的可编辑可执行文件 批处理:可以对某一对象批量操作的文件 二.批处理命令简介 命令1~10 1 echo 和 @ 回显命令 @ #关闭单行回显 ...
- myeclipse与tomcat,运行jsp程序
一.myeclipse中配置JRE 步骤: 1.选择window->preferences->Java->Installed JREs 2.点击窗口右边的"add" ...
- [noi34]palindrome
分割实际上就是不断地从两端取出一样的一段,并对剩下的串进行分割.下面我们来证明一下每一次贪心取出最短一段的正确性: 考虑两种分割方式,分别表示成S=A+B+A和S=C+D+C,其中A就是最短的一段,那 ...
- Python+selenium定位一组元素,复选框
- 面试官问我HTTP,我真的是
面试官:今天要不来聊聊HTTP吧? 候选者:嗯,HTTP「协议」是客户端和服务器「交互」的一种通迅的格式 候选者:所谓的「协议」实际上就是双方约定好的「格式」,让双方都能看得懂的东西而已 候选者:所谓 ...
- 【.NET 与树莓派】MPD 的 Mini-API 封装
在前面的水文中,一方面,老周向各位同学介绍了通过 TCP 连接来访问 MPD 服务:另一方面,也简单演示了 ASP.NET Core 的"极简 API"(Mini API).本篇老 ...
- jQuery源码中的赌博网站
前言 jQuery源码中有赌博网站? 起因是公司发的一份自查文件,某银行在日常安全运营过程中发现在部分jQuery源码中存在赌博和黄色网站链接. 链接分为好几个: www.cactussoft.cn ...
- Topcoder 14719 - RatingProgressAward(最小割)
题面传送门 神仙最小割--好久没写过网络流了,故写题解以祭之( 首先考虑一个非常 trivial 的问题:如果知道排列顺序之后怎样计算最大值,用脚趾头想一下就能知道是原序列的最大子段和,因为每个课程之 ...