Output of C++ Program | Set 5
Difficulty Level: Rookie
Predict the output of below C++ programs.
Question 1
1 #include<iostream>
2 using namespace std;
3
4 class Test
5 {
6 int value;
7 public:
8 Test(int v);
9 };
10
11 Test::Test(int v)
12 {
13 value = v;
14 }
15
16 int main()
17 {
18 Test t[100];
19 return 0;
20 }
Output: Compiler error
The class Test has one user defined constructor “Test(int v)” that expects one argument. It doesn’t have a constructor without any argument as the compiler doesn’t create the default constructor if user defines a constructor (See this).
Following modified program works without any error.
1 #include<iostream>
2 using namespace std;
3
4 class Test
5 {
6 int value;
7 public:
8 Test(int v = 0);
9 };
10
11 Test::Test(int v)
12 {
13 value = v;
14 }
15
16 int main()
17 {
18 Test t[100];
19 return 0;
20 }
Question 2
1 #include<iostream>
2 using namespace std;
3 int &fun()
4 {
5 static int a = 10;
6 return a;
7 }
8
9 int main()
10 {
11 int &y = fun();
12 y = y +30;
13 cout << fun();
14 return 0;
15 }
Output: 40
The program works fine because ‘a’ is static. Since ‘a’ is static, memory location of it remains valid even after fun() returns. So a reference to static variable can be returned.
Question 3
1 #include<iostream>
2 using namespace std;
3
4 class Test
5 {
6 public:
7 Test();
8 };
9
10 Test::Test()
11 {
12 cout<<"Constructor Called \n";
13 }
14
15 int main()
16 {
17 cout<<"Start \n";
18 Test t1();
19 cout<<"End \n";
20 return 0;
21 }
Output:
Start
End
Note that the line “Test t1();” is not a constructor call. Compiler considers this line as declaration of function t1 that doesn’t recieve any parameter and returns object of type Test.
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 15:23:22
Output of C++ Program | Set 5的更多相关文章
- 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 14
Predict the output of following C++ program. Difficulty Level: Rookie Question 1 1 #include <iost ...
- 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 ...
随机推荐
- Qt 信号与槽的自动关联机制
前言 对于一些简单的事件判别,如点击按钮.无需写代码关联信号和槽函数. connect(ui->Btnshowhello,SIGNAL(clicked(bool)),this,SLOT(Btns ...
- Python 操作 Redis 发布订阅
Python 操作 Redis 发布订阅 介绍 Redis可以通过多个客户机订阅相同的频道,一个服务机在相应频道进行发布,从而实现在客户机收听服务机发布相应信息,可以利用这个机制实现多个客户机之间的信 ...
- 关于linux系统密码策略的设置
由于工作需要最近需要将公司的多台linux服务器进行密码策略的设置,主要内容是增加密码复杂度. 操作步骤如下,不会的同学可以参考: 操作前需要掌握如下几个简单的知识点:(其实不掌握也行,不过学学没坏处 ...
- Linux(kali)基础设置
本笔记的友情链接 常用目录介绍 boot 存放启动文件 dev 存放设备文件 etc 存放配置文件 home 普通用户家目录,以/home/$username的方式存放 media 移动存储自动挂载目 ...
- JVM 是用什么语言写的?
JAVA中就虚拟机是其它语言开发的,用的是C语言+汇编语言 基于此之上就是JAVA本身了 虚拟机只起到解析作用另外,JAVA并不比C语言慢,说JAVA慢一般是九十年代那时候的JAVA, 而现在 在 ...
- 带allow-create的el-select限制长度
需求:给el-select添加新增字段长度限制且新增内容不能为空 1.首先给el-select绑定一个id(例如:selectSku),这个id会传到组件里面,绑定在那个input上面, <el ...
- [atAGC054C]Roughly Sorted
考虑对于确定的排列$\{p_{i}\}$,如何求出其(交换后)会得到的排列-- 令$cnt_{x}$为在$i$之前比$x$大的元素个数(其中$p_{i}=x$),显然排列合法当且仅当$cnt_{i}\ ...
- [bzoj4777]Switch Grass
结论:最短路径一定是单独的一条边且在最小生成树上,可以用反证法证明.那么求出最小生成树,对于每一个点建立一棵权值线段树,再对每一个权值线段树上的叶子节点开一个multiset,维护所有儿子中该种颜色的 ...
- [loj3313]序列
定义$C_{i}$表示令$i,i+1,i+2,...$的位置减1的操作,定义$I_{i}$表示令$i,i+2,i+4,...$的位置减1的操作 结论1:一定存在一种最优解使得$\forall i$不同 ...
- LifseaOS 悄然来袭,一款为云原生而生的 OS
作者:黄韶宇.初扬 审核&校对:溪洋.海珠 编辑&排版:雯燕 LifseaOS 在刚刚过去的云栖大会上,一款新的 Linux Base 操作系统悄悄发布,它就是 LifseaOS(Li ...