Output of C++ Program | Set 4
Difficulty Level: Rookie
Predict the output of below C++ programs.
Question 1
1 #include<iostream>
2 using namespace std;
3
4 int x = 10;
5 void fun()
6 {
7 int x = 2;
8 {
9 int x = 1;
10 cout << ::x << endl;
11 }
12 }
13
14 int main()
15 {
16 fun();
17 return 0;
18 }
Output: 10
If Scope Resolution Operator is placed before a variable name then the global variable is referenced. So if we remove the following line from the above program then it will fail in compilation.
1 int x = 10;
Question 2
1 #include<iostream>
2 using namespace std;
3
4 class Point
5 {
6 private:
7 int x;
8 int y;
9 public:
10 Point(int i, int j); // Constructor
11 };
12
13 Point::Point(int i = 0, int j = 0)
14 {
15 x = i;
16 y = j;
17 cout << "Constructor called";
18 }
19
20 int main()
21 {
22 Point t1, *t2;
23 return 0;
24 }
Output: Constructor called.
If we take a closer look at the statement “Point t1, *t2;:” then we can see that only one object is constructed here. t2 is just a pointer variable, not an object.
Question 3
1 #include<iostream>
2 using namespace std;
3
4 class Point
5 {
6 private:
7 int x;
8 int y;
9 public:
10 Point(int i = 0, int j = 0); // Normal Constructor
11 Point(const Point &t); // Copy Constructor
12 };
13
14 Point::Point(int i, int j)
15 {
16 x = i;
17 y = j;
18 cout << "Normal Constructor called\n";
19 }
20
21 Point::Point(const Point &t)
22 {
23 y = t.y;
24 cout << "Copy Constructor called\n";
25 }
26
27 int main()
28 {
29 Point *t1, *t2;
30 t1 = new Point(10, 15);
31 t2 = new Point(*t1);
32 Point t3 = *t1;
33 Point t4;
34 t4 = t3; //assignment operator
35 return 0;
36 }
Output:
Normal Constructor called
Copy Constructor called
Copy Constructor called
Normal Constructor called
See following comments for explanation:
1 Point *t1, *t2; // No constructor call
2 t1 = new Point(10, 15); // Normal constructor call
3 t2 = new Point(*t1); // Copy constructor call
4 Point t3 = *t1; // Copy Constructor call
5 Point t4; // Normal Constructor call
6 t4 = t3; // Assignment operator call
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:16:34
Output of C++ Program | Set 4的更多相关文章
- 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 ...
随机推荐
- 深入浅出:了解时序数据库 InfluxDB
数据模型 1.时序数据的特征 时序数据应用场景就是在时间线上每个时间点都会从多个数据源涌入数据,按照连续时间的多种纬度产生大量数据,并按秒甚至毫秒计算的实时性写入存储. 传统的RDBMS数据库对写入的 ...
- svn与git区别
代码扫描工具介绍:https://baijiahao.baidu.com/s?id=1629218655164599200&wfr=spider&for=pc Git和SVN的区别与联 ...
- Linux配置2个Tomcat同时运行
先说一下怎么遇到的这个问题,在练习linux中部署web项目时,linux系统安装了两个tomcat. 操作步骤: 1.配置profile#vi /etc/profile 输入以下内容: 这是两个to ...
- [jmeter]Jmeter+ant实现接口自动化
1.安装jmeter 和ant &环境变量配置百度去~ 2.jmeter和ant关联 &将JMeter所在目录下extras子目录里的ant-JMeter-1.1.1.jar复制到an ...
- [linux]centos7.4上安装MySQL-8.0.11【完美安装】
版本声明 centos7.4 MySQL-8.0.11 1.我用的阿里云的虚拟主机,刚从windows换到linux,需要装下常用工具 #安装下sz rz常用到上传下载的命令 yum install ...
- js实现全选与全部取消功能
function checkAll() { //把所有参与选择的checkbox使用相同的name,这里为"num_iid" var eles = document.getE ...
- Linux基础五:网络配置与管理
五.网络配置与管理 1.网络知识 2.命令 ifconfig命令 <=> ip addr show 命令--查看本地所有网卡配置信息 ens32:本地以太网网卡,lo:本地回环网卡 ...
- [luogu5344]逛森林
由于没有删边操作,可以先建出整棵森林,之后再用并查集判断是否连通,若连通必然与最后的森林相同 但如果用树链剖分+线段树的形式来优化建图,更具体如下: 建立两颗线段树,左边从儿子连向父亲,右边从父亲连向 ...
- 测试平台系列(80) 封装Redis客户端
大家好~我是米洛! 我正在从0到1打造一个开源的接口测试平台, 也在编写一套与之对应的完整教程,希望大家多多支持. 欢迎关注我的公众号测试开发坑货,获取最新文章教程! 回顾 上一节我们编写了Redis ...
- Go语言核心36讲(Go语言实战与应用十二)--学习笔记
34 | 并发安全字典sync.Map (上) 我们今天再来讲一个并发安全的高级数据结构:sync.Map.众所周知,Go 语言自带的字典类型map并不是并发安全的. 前导知识:并发安全字典诞生史 换 ...