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的更多相关文章

  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. namespace之cgroup

    Linux Namespace,但是Namespace解决的问题主要是环境隔离的问题,这只是虚拟化中最最基础的一步,我们还需要解决对计算机资源使用上的隔离.也就是说,虽然你通过Namespace把我J ...

  2. 2020美亚团体—Daniel篇

    Daniel的桌上计算机的哈希值(SHA-256)是甚么? 通过取证大师计算 SHA-256值为 07DD40CF28603F421F3A09CD38F1C8AA40A2AC4BFB46ECF8299 ...

  3. vuex配置token和用户信息

    首先设计的是登录成功后端产生token,前端取出放在Local Storage,便于后面每个请求默认带上这里的token以及取用户相关信息 和main.js同级建store.js文件,代码如下 imp ...

  4. 常见yaml写法-deployment

    apiVersion: extensions/v1beta1 #接口版本 kind: Deployment #接口类型 metadata: name: cango-demo #Deployment名称 ...

  5. 大一C语言学习笔记(6)---自省篇--流程控制;break,continue,return间的异同;数组应用到循环语句中需要注意的问题;++i 和 i++的异同等。

    下面是傻瓜博主"曾经"犯过的错和一些心得:        ༼ つ ◕_◕ ༽つ 1.要想流程控制学好,一定要学会化繁为简,举栗子: 三目运算符 (略?略:略)---就是一个数字嘛, ...

  6. Python基础(作用域)

    def _private_1(name): return 'Hello, %s' % name def _private_2(name): return 'Hi, %s' % name def gre ...

  7. JSRE中的多任务与多线程

    前言 ​ 这几天在爱智官网看了下JSRE其他的Api,看了一个比较有意思的模块 - 多任务模块task,大致看了下他们的接口说明和案例,感觉和多线程差不多,然后就准备去看下实现方式,找了很久没有找到源 ...

  8. Java是“值传递”还是“引用传递”?

    本文是根据stackoverflow网站上的一道题目整理出来的. Java一直是"值传递"而不是引用传递.看一个例子: public class Main{ public stat ...

  9. <C#任务导引教程>练习四

    //27,创建一个控制台应用程序,声明两个DateTime类型的变量dt,获取系统的当前日期时间,然后使用Format格式化进行规范using System;class Program{    sta ...

  10. [cf1361E]James and the Chase

    称一个点是"好点",当且仅当其到其余所有点恰存在一条简单路径 结论1:$x$为好点当且仅当以$x$为根的dfs树包含所有点且非树边均为返祖边 若不包含所有点,那么$x$到不被包含的 ...