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 ...
随机推荐
- Kubernetes(k8s)部署redis-cluster集群
Redis Cluster 提供了一种运行 Redis 安装的方法,其中数据 在多个 Redis 节点之间自动分片. Redis Cluster 还在分区期间提供了一定程度的可用性,这实际上是在某些节 ...
- Redis6.2发布 地理位置功能增强了什么?
原文地址:https://developer.aliyun.com/article/780257 Redis社区最近刚刚发布Redis6.2 RC1版本,在本次发布中,阿里云Tair团队(阿里云云内存 ...
- 攻防世界 WEB 高手进阶区 XCTF 4th-CyberEarth ics-06 Writeup
攻防世界 WEB 高手进阶区 XCTF 4th-CyberEarth ics-06 Writeup 题目介绍 题目考点 掌握暴力破解手段 Writeup 打开链接 http://220.249.52. ...
- linux下go环境配置
环境申明: centos 7.4 1.go下载最新的版本(linux) 下载本地后rz到服务器,然后tar -zxvf go1.9.2.linux-amd64.tar.gz 解压出go文件放在 ...
- docker容器运行java后台程序,存到数据库的时间差一天的问题
主要原因是docker容器中的时间用的是标准时间,不是用的宿主机的时间. 修改方法: docker run -e TZ="Asia/Shanghai" -d -p 80:80 -- ...
- 大爽Python入门教程 1-3 简单的循环与判断
大爽Python入门公开课教案 点击查看教程总目录 这里只初步认识下循环和判断,以便于我们去实现一些简单的计算. 循环和判断的详细知识和细节,我们将在后面的章节(大概是第三章)展开阐述. 1 初步了解 ...
- 菜鸡的Java笔记 第十七 static 关键字
static 是java中定义的一个关键字,主要是描述全局的概念,所以利用static关键字可以定义属性,定义方法 但是在90%的情况下,我们的开发代码很少会去直接编写static*// ...
- APM监控--(三)zipkin部署手册
一,基础知识储备分布式跟踪的目标一个分布式系统由若干分布式服务构成,每一个请求会经过多个业务系统并留下足迹,但是这些分散的数据对于问题排查,或是流程优化都很有限,要能做到追踪每个请求的完整链路调用,收 ...
- Java安全之基于Tomcat的通用回显链
Java安全之基于Tomcat的通用回显链 写在前面 首先看这篇文还是建议简单了解下Tomcat中的一些概念,不然看起来会比较吃力.其次是回顾下反射中有关Field类的一些操作. * Field[] ...
- Python之用型号构成一个三角形代码
#coding=utf-8 #******直角三角形*********** #左下角三角形 for i in range(1,6): print '*'*i print "=&quo ...