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. 服务集与AP的配合

    一.实验目的 1)掌握添加无线网络配置 2)掌握配置信道和协议使用并配置在一个天线上同时运行两个服务集,即两个无线网络 二.实验仪器设备及软件 仪器设备:一台AC,两台AP,一台AR,一台LSW 软件 ...

  2. js深拷贝你还不会吗

    js深拷贝 在讲正题之前我们要先了解数据存储的方式 数据存储方式 在讲之前我们要先知道值类型和引用类型的存储方式. 在JavaScript数据类型中有两种数据类型. 值类型:字符串(String).数 ...

  3. java注解@Transactional事务类内调用不生效问题及解决办法

    @Transactional 内部调用例子 在 Spring 的 AOP 代理下,只有目标方法由外部调用,目标方法才由 Spring 生成的代理对象来管理,这会造成自调用问题.若同一类中的其他没有@T ...

  4. 注解@RunWith的作用

    @RunWith就是一个运行器 @RunWith(JUnit4.class)就是指用JUnit4来运行 @RunWith(SpringJUnit4ClassRunner.class),让测试运行于Sp ...

  5. 菜鸡的Java笔记 java数据库编程(JDBC)

    java数据库编程(JDBC)        介绍 JDBC 的基本功能            content (内容)        现在几乎所有的项目开发过程之中都不可能离开数据库,所以在java ...

  6. [hdu4747]Mex

    首先计算出以1为左端点的所有区间的mex,考虑删除左端点仍然维护这个序列:设当前删除点下一次出现在y,y~n的mex不变,从左端点到y的点中大于删除值的点要变成删除值,因为这个是不断递增的,所以是一段 ...

  7. [源码解析] PyTorch 分布式(9) ----- DistributedDataParallel 之初始化

    [源码解析] PyTorch 分布式(9) ----- DistributedDataParallel 之初始化 目录 [源码解析] PyTorch 分布式(9) ----- DistributedD ...

  8. 实用QPS和TPS高的高效分析方法

    现在主库的MySQL的QPS一直在3K/s左右,想知道其到底执行了那些SQL,或者是那些SQL执行的次数比较多: 腾讯云的后台监控: 开启腾讯云的SQL审计后,下载几分钟SQL日志文件, 下列语句在M ...

  9. AotucCrawler 快速爬取图片

    AotucCrawler 快速爬取图片 今天介绍一款自动化爬取图片项目. GitHub: https://github.com/YoongiKim/AutoCrawler Google, Naver ...

  10. 学以致用 | Redis概念与简单实操

    Redis概念 Redis是一个由C语言编写.基于key-value存储结构的开源NoSQL数据库,其读写速度为10万次/秒,这个速度已经远远大于传统的关系型数据库. 使用场景 在高并发的情况下,可将 ...