Output of C++ Program | Set 14
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的更多相关文章
- Output of C++ Program | Set 18
Predict the output of following C++ programs. Question 1 1 #include <iostream> 2 using namespa ...
- Output of C++ Program | Set 17
Predict the output of following C++ programs. Question 1 1 #include <iostream> 2 using namespa ...
- Output of C++ Program | Set 16
Predict the output of following C++ programs. Question 1 1 #include<iostream> 2 using namespac ...
- Output of C++ Program | Set 15
Predict the output of following C++ programs. Question 1 1 #include <iostream> 2 using namespa ...
- Output of C++ Program | Set 13
Predict the output of following C++ program. 1 #include<iostream> 2 using namespace std; 3 4 c ...
- Output of C++ Program | Set 11
Predict the output of following C++ programs. Question 1 1 #include<iostream> 2 using namespac ...
- Output of C++ Program | Set 9
Predict the output of following C++ programs. Question 1 1 template <class S, class T> class P ...
- 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 ...
- Output of C++ Program | Set 6
Predict the output of below C++ programs. Question 1 1 #include<iostream> 2 3 using namespace ...
随机推荐
- 『学了就忘』Linux基础命令 — 22、Linux中的硬链接和软链接
目录 1.文件和目录的基本存储 2.In命令介绍 (1)我们来看看ln命令的基本信息 (2)ln命令的基本格式 3.创建硬链接 (1)如何创建硬链接 (2)硬链接特征 (3)硬连接原理 4.创建软链接 ...
- Downward API —— 在容器内部获取 Pod 信息
我们知道,每个 Pod 在被超过创建出来之后,都会被系统分配唯一的名字.IP地址,并且处于某个 Namespace 中,那么我们如何在 Pod 的容器内获取 Pod 的这些重要信息呢? 答案就是使用 ...
- Java测试开发--Maven用法(三)
一.Maven简介 Maven 是java项目构建工具,统一包的管理,统一项目管理.项目编译,测试打包.部署. 二.Maven工程搭建: 1.新建maven工程,如下图 2. 新建工程后,jdk使用的 ...
- JMeter学习笔记--并发登录测试
账号密码读取文件 1.设置线程数为30,并发用户量就是30个用户同时登录 2.添加同步定时器 添加 Synchronizing Timer 同步定时器,为了阻塞线程,当线程数达到指定数量,再同时释放, ...
- git rebase 合并提交
git rebase 合并提交 合并最近多次提交记录 语法 git rebase -i HEAD~n 1.进入合并模式 合并最近三次提交 git rebase -i HEAD~3 然后你会看到一个像下 ...
- RDD的缓存
RDD的缓存/持久化 缓存解决的问题 缓存解决什么问题?-解决的是热点数据频繁访问的效率问题 在Spark开发中某些RDD的计算或转换可能会比较耗费时间, 如果这些RDD后续还会频繁的被使用到,那么可 ...
- 从零开始制作一个linux iso镜像
一.前言 对于一个极简化的linux系统而言,只需要三个部分就能组成,它们分别是一个linux内核.一个根文件系统和引导.以下是本文制作linux iso镜像所用到的系统和软件: OS ...
- ES6基础知识(Reflect)
<!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF-8&quo ...
- Ubuntu軟件商店加載失敗的解決方法
Ubuntu軟件商店有的时候加载不出来,先用下面的试试 sudo apt install python-apt 如果不行,继续下面的方法 打开终端,运行下面的命令: sudo apt-get upda ...
- [atAGC045F]Division into Multiples
令$d=\gcd(a,b)$,可以发现$c|(ax+by)$等价于$lcm(c,d)|(ax+by)$,因此不妨令$c'=lcm(c,d)$,然后将$a$.$b$和$c$同时除以$d$ 接下来设$(a ...