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. 『学了就忘』Linux基础命令 — 27、搜索操作相关命令

    目录 1.whereis命令 2.which命令 3.locate命令 1.whereis命令 whereis是搜索系统命令的命令,也就是说,whereis命令不能搜索普通文件,而只能搜索系统命令. ...

  2. ELK集群之elasticsearch(3)

    Elasticsearch-基础介绍及索引原理分析 介绍 Elasticsearch 是一个分布式可扩展的实时搜索和分析引擎,一个建立在全文搜索引擎 Apache Lucene(TM) 基础上的搜索引 ...

  3. 【java+selenium3】特殊元素iframe的定位及详解(三)

    一.iframe 内联框架 1.自己写个网页,仅供理解iframe演示使用,如下 <!DOCTYPE html> <html> <head> <meta ch ...

  4. LeetCode 重排链表 OPPO笔试

    重排链表 几个关键点: 1. 双指针(快慢指针找中点)(用于反转后一部分) 2. 反转后一部分 (reverse函数) 3. 合并链表 合并的时候在笔试的时候想了一种比我之前想的简单的方法 从slow ...

  5. 庆祝dotnet6,fastgithub送给你

    前言 dotnet6正式发布了,fastgithub是使用dotnet开发的一款github加速器,作为开发者,无人不知github,作为github用户,fastgithub也许是你不可或缺的本机工 ...

  6. Linux mem 2.5 Buddy 内存回收机制

    文章目录 1. 简介 2. LRU 组织 2.1 LRU 链表 2.2 LRU Cache 2.3 LRU 移动操作 2.3.1 page 加入 LRU 2.3.2 其他 LRU 移动操作 3. LR ...

  7. RDD的缓存

    RDD的缓存/持久化 缓存解决的问题 缓存解决什么问题?-解决的是热点数据频繁访问的效率问题 在Spark开发中某些RDD的计算或转换可能会比较耗费时间, 如果这些RDD后续还会频繁的被使用到,那么可 ...

  8. 在随着layui官网下架后 layui镜像站起来了

    layui:https://gitee.com/lh_yun/layui 介绍 layui镜像站 「本站仅为 layui 文档保留的镜像站点,与官方无关」 源码地址 在线 pdf 1.主页 https ...

  9. C# 获取PDF中的数字签名证书

    PDF中的加数字签名是对文档权威性的有效证明.我们在向PDF文档添加签名时,需要准备可信任的签名证书.同时,对已有的签名,可验证签名是否有效,也可以获取文档中的签名证书信息.下面,以C#代码示例展示如 ...

  10. 【Microsoft Azure 的1024种玩法】三.基于Azure云平台构建Discuz论坛

    [简介] Discuz!是一套通用社区论坛软件系统,用户在不需要任何编程的基础上,通过简单的设置和安装,在互联网上搭建起具备完善功能.很强负载能力和可高度定制的论坛服务. [前期文章] [操作步骤] ...