Predict the output of following C++ program.

  Difficulty Level: Rookie

Question 1

  1. 1 #include <iostream>
  2. 2 using namespace std;
  3. 3
  4. 4 class A
  5. 5 {
  6. 6 int id;
  7. 7 public:
  8. 8 A (int i)
  9. 9 {
  10. 10 id = i;
  11. 11 }
  12. 12 void print()
  13. 13 {
  14. 14 cout << id << endl;
  15. 15 }
  16. 16 };
  17. 17
  18. 18 int main()
  19. 19 {
  20. 20 A a[2];
  21. 21 a[0].print();
  22. 22 a[1].print();
  23. 23 return 0;
  24. 24 }

  There is a compilation error in line “A a[2]“. There is no default constructor in class A. When we write our own parameterzied constructor or copy constructor, compiler doesn’t create the default constructor (See this Gfact). We can fix the error, either by creating a default constructor in class A, or by using the following syntax to initialize array member using parameterzied constructor.

  1. // Initialize a[0] with value 10 and a[1] with 20
  2. A a[2] = { A(10), A(20) }

Question 2

  1. 1 #include <iostream>
  2. 2 using namespace std;
  3. 3
  4. 4 class A
  5. 5 {
  6. 6 int id;
  7. 7 static int count;
  8. 8 public:
  9. 9 A()
  10. 10 {
  11. 11 count++;
  12. 12 id = count;
  13. 13 cout << "constructor called " << id << endl;
  14. 14 }
  15. 15 ~A()
  16. 16 {
  17. 17 cout << "destructor called " << id << endl;
  18. 18 }
  19. 19 };
  20. 20
  21. 21 int A::count = 0;
  22. 22
  23. 23 int main()
  24. 24 {
  25. 25 A a[2];
  26. 26 return 0;
  27. 27 }

  Output:

  constructor called 1
  constructor called 2
  destructor called 2
  destructor called 1
  In the above program, object a[0] is created first, but the object a[1] is destroyed first. Objects are always destroyed in reverse order of their creation. The reason for reverse order is, an object created later may use the previously created object.

  For example, consider the following code snippet.

  1. A a;
  2. B b(a);

  In the above code, the object ‘b’ (which is created after ‘a’), may use some members of ‘a’ internally. So destruction of ‘a’ before ‘b’ may create problems. Therefore, object ‘b’ must be destroyed before ‘a’.

Question 3

  1. 1 #include <iostream>
  2. 2 using namespace std;
  3. 3
  4. 4 class A
  5. 5 {
  6. 6 int aid;
  7. 7 public:
  8. 8 A(int x)
  9. 9 {
  10. 10 aid = x;
  11. 11 }
  12. 12 void print()
  13. 13 {
  14. 14 cout << "A::aid = " << aid;
  15. 15 }
  16. 16 };
  17. 17
  18. 18 class B
  19. 19 {
  20. 20 int bid;
  21. 21 public:
  22. 22 static A a;
  23. 23 B (int i)
  24. 24 {
  25. 25 bid = i;
  26. 26 }
  27. 27 };
  28. 28
  29. 29 int main()
  30. 30 {
  31. 31 B b(10);
  32. 32 b.a.print();
  33. 33 return 0;
  34. 34 }

  Compiler Error: undefined reference to `B::a’
  The class B has a static member ‘a’. Since member ‘a’ is static, it must be defined outside the class. Class A doesn’t have Default constructor, so we must pass a value in definition also. Adding a line “A B::a(10);” will make the program work.

  The following program works fine and produces the output as “A::aid = 10″

  1. 1 #include <iostream>
  2. 2 using namespace std;
  3. 3
  4. 4 class A
  5. 5 {
  6. 6 int aid;
  7. 7 public:
  8. 8 A(int x)
  9. 9 {
  10. 10 aid = x;
  11. 11 }
  12. 12 void print()
  13. 13 {
  14. 14 cout << "A::aid = " <<aid;
  15. 15 }
  16. 16 };
  17. 17
  18. 18 class B
  19. 19 {
  20. 20 int bid;
  21. 21 public:
  22. 22 static A a;
  23. 23 B (int i)
  24. 24 {
  25. 25 bid = i;
  26. 26 }
  27. 27 };
  28. 28
  29. 29 A B::a(10);
  30. 30
  31. 31 int main()
  32. 32 {
  33. 33 B b(10);
  34. 34 b.a.print();
  35. 35 return 0;
  36. 36 }

  

  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  16:22:31

Output of C++ Program | Set 14的更多相关文章

  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 13

    Predict the output of following C++ program. 1 #include<iostream> 2 using namespace std; 3 4 c ...

  6. Output of C++ Program | Set 11

    Predict the output of following C++ programs. Question 1 1 #include<iostream> 2 using namespac ...

  7. Output of C++ Program | Set 9

    Predict the output of following C++ programs. Question 1 1 template <class S, class T> class P ...

  8. 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 ...

  9. Output of C++ Program | Set 6

    Predict the output of below C++ programs. Question 1 1 #include<iostream> 2 3 using namespace ...

随机推荐

  1. ansible安装和批量执行命令

    yum install -y ansible 编辑 /etc/ansible/hosts 文件 # This is the default ansible 'hosts' file.## It sho ...

  2. 【Go语言学习笔记】函数做参数和闭包

    函数做参数 在Go语言中,函数也是一种数据类型,我们可以通过type来定义它,它的类型就是所有拥有相同的参数,相同的返回值的一种类型.类似于重写(同名覆盖). 回调函数:函数有一个参数是函数类型,这个 ...

  3. Go websocket EOF bug

    背景 使用的 golang.org/x/net/websocket 包,前端一发来消息就报错 if err = websocket.Message.Receive(ws, &msg); err ...

  4. RedHat 7.0 Linux 下划分区,分区加密,配额,逻辑卷管理

    1:如何划分区: 1:明确分区的对象:xxx :fdisk /dev/xxx 2:增加一个分区:n:选择主分区或者扩展分区,"p" or "e" :默认地方开始 ...

  5. 深入剖析 RocketMQ 源码 - 消息存储模块

    一.简介 RocketMQ 是阿里巴巴开源的分布式消息中间件,它借鉴了 Kafka 实现,支持消息订阅与发布.顺序消息.事务消息.定时消息.消息回溯.死信队列等功能.RocketMQ 架构上主要分为四 ...

  6. 【Python接口自动化测试】Postman使用简介

    下载地址: http://www.downza.cn/soft/205171.html 工具栏 New: 新建,可以新建Request请求,Collection请求集,环境等等 Import: 导入, ...

  7. js中的特数值-null-undefined-NaN

    一.补充 1.js中的三大特殊数据:undefined.null.NaN NaN :非法的数值运算得到的结果 特殊之处: 是一个数值型数据,但不是一个数字 NaN不等于任何值,和任何数据都不相等,Na ...

  8. VIM处理工具与正则表达式

    *本文中/data目录为训练目录 1.在vim中设置TAB缩进为四个字符 打开vim 输入:set tabstop=4 2.复制/etc/rc.d/init.d/functions文件至/tmp/,替 ...

  9. Windows内核中的CPU架构-8-任务段TSS(task state segment)

    Windows内核中的CPU架构-8-任务段TSS(task state segment) 任务段tss(task state segment)是针对于CPU的一个概念. 举一个简单的例子,你一个电脑 ...

  10. vue+node+mongondb实战之mongodb登陆操作

    页面搭建基本完成,只是样式还没有美化,由于采取的前后端分离开发,所有页面逻辑全部由vue来负责,后台采用express框架只用来提供 接口,注册就是讲数据存入数据库,比较简单,而登陆碰了一些小问题,发 ...