Predict the output of following C++ program.

  Difficulty Level: Rookie

Question 1

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

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

Question 2

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

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

    题目:备份是个好习惯 思路分析 打开题目,看到一个字符串. 联系到题目,就猜到肯定是源代码泄露,用工具扫一下,发现了index.php.bak,验证了我的猜想,下载下来看看. <?php /** ...

  2. C++ pragma once 与 ifndef 用法区别

    #pragma once 与 #ifndef 的作用 (1)在C/C++中,在使用预编译指令#include的时候,为了防止重复引用造成二义性. (2)在能够支持这两种方式的编译器上,二者并没有太大的 ...

  3. 【数据结构&算法】11-树基础&二叉树遍历

    目录 前言 树的定义 树的存储结构 双亲表示法 孩子表示法 孩子兄弟表示法 二叉树 定义 特点 形态 特殊二叉树 斜树 满二叉树 完全二叉树 二叉树的性质 二叉树的存储结构 二叉树的顺序存储结构 二叉 ...

  4. FZU ICPC 2020 寒假训练 6 —— 字符串处理

    P1603 斯诺登的密码 题目描述 2013年X月X日,俄罗斯办理了斯诺登的护照,于是他混迹于一架开往委内瑞拉的飞机.但是,这件事情太不周密了,因为FBI的间谍早已获悉他的具体位置--但这不是最重要的 ...

  5. Linux——搭建Samba(CIFS)服务器

    一.Samba的基本概念 Samba服务:是提供基于Linux和Windows的共享文件服务,服务端和客户端都可以是Linux或Windows操作系统.可以基于特定的用户访问,功能比NFS更强大. S ...

  6. [luogu5574]任务分配问题

    首先暴力dp,令$f_{i,j}$表示前$i$个点划分为$j$段,即有转移$f_{i,j}=\min f_{k-1,j-1}+calc(k,i)$(其中$calc(i,j)$表示求区间$[i,j]$的 ...

  7. 应用程序池自动停止,事件查看器报错6D000780

    20210913 今天中午网站突然报错,后台程序无法访问,503错误. 调查发现"应用程序池"被关闭,但是手动开启后不久,又被关闭. 本地调试没问题,所以一开始怀疑是服务器或者Ng ...

  8. [Codeforces Global Round 14]

    打挺差的. 不过\(C,D\)一眼秒了,大概是对这几个月努力的一个结果? \(B\)玄学错误挂了两发. 脑子痛然后打到一半就去睡觉了. -------------------------------- ...

  9. Atcoder Grand Contest 015 F - Kenus the Ancient Greek(找性质+乱搞)

    洛谷题面传送门 & Atcoder 题面传送门 一道难度 Au 的 AGC F,虽然看过题解之后感觉并不复杂,但放在现场确实挺有挑战性的. 首先第一问很简单,只要每次尽量让"辗转相除 ...

  10. C/C++内存几大分区和存储空间的布局

    先看一下可执行文件加载进内存后形成的进程在内存中的结构,如下图: 代码区:存放CPU执行的机器指令,代码区是可共享,并且是只读的. 数据区:存放已初始化的全局变量.静态变量(全局和局部).常量数据. ...