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. SpringCloud 2020.0.4 系列之 Bus

    1. 概述 老话说的好:会休息的人才更会工作,身体是革命的本钱,身体垮了,就无法再工作了. 言归正传,之前我们聊了 SpringCloud 的 分布式配置中心 Config,文章里我们聊了config ...

  2. Redis去重方法

    目录 1.基于 set 2.基于 bit 3.基于 HyperLogLog 4. 基于bloomfilter 这篇文章主要介绍了Redis实现唯一计数的3种方法分享,本文讲解了基于SET.基于 bit ...

  3. ONVIF协议客户端

    前几天跟大家聊了一些关于ONVIF的一些基础知识,它的工作原理以及优势.今天安徽思蔷信息科技为带大家了解一下simpleonvif 百度云盘下载地址:链接:https://pan.baidu.com/ ...

  4. Cannot load module file xxx.iml的两种解决方法

    一. 一种是点击左上角File,然后点击Invalidate Caches / Restart...,弹出对话框再点击Invalidate and Restart等待工程重新加载,问题就解决了. 二. ...

  5. PTA 7-4 最小生成树的唯一性 (35分)

    PTA 7-4 最小生成树的唯一性 (35分) 给定一个带权无向图,如果是连通图,则至少存在一棵最小生成树,有时最小生成树并不唯一.本题就要求你计算最小生成树的总权重,并且判断其是否唯一. 输入格式: ...

  6. ES6模块化引入

    //a.js 导出的关键字 export export let str = "laowang"; export function add(a,b){ return a + b ; ...

  7. Part 23 to 26 Routing in Angular

    Part 23 AngularJS routing tutorial In general, as the application becomes complex you will have more ...

  8. c++学习笔记3(动态内存分配)

    为了效率,需要按需去进行动态内存分配,在c中,可以用malloc去实现动态内存分配,而在c++中,则用new运算符去实现. 用法一:动态分配一个变量的存储空间 p=new T T为类型名 P则为T*类 ...

  9. 菜鸡的Java笔记 第十七 static 关键字

    static 是java中定义的一个关键字,主要是描述全局的概念,所以利用static关键字可以定义属性,定义方法        但是在90%的情况下,我们的开发代码很少会去直接编写static*// ...

  10. 从零搭建vue3.0项目架构(附带代码、步骤详解)

    前言: GitHub上我开源了vue-cli.vue-cli3两个库,文章末尾会附上GitHub仓库地址.这次把2.0的重新写了一遍,优化了一下.然后按照2.0的功能和代码,按照vue3.0的语法,完 ...