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 ...
随机推荐
- lumen、laravel问题汇总
框架报500 1.chmod 777 -R storage 将日志目录权限设置下. 2.修改fastcgi,将代码目录包含进去. fastcgi_param PHP_ADMIN_VALUE " ...
- 2016-12-01,我的CSDN有排名啦!
等了好久终于等到今天,梦了好久终于把梦实现----[捂脸] 从2015-08-03发表第一篇博客以来,这里就成了我记录技术成长点滴,学习过程,总结备忘的地方,虽然中间自己也在腾讯云上搭过自己的博客,但 ...
- testNG 注解使用说明
1.TestNG常用注解 @BeforeSuite 标记的方法:在某个测试套件(suite)开始之前运行 @BeforeTest 在某个测试(test)开始之前运行 @BeforeClass 在某个测 ...
- NOIP模拟92&93(多校26&27)
前言 由于太菜了,多校26 只改出来了 T1 ,于是直接并在一起写啦~~~. T0 NOIP 2018 解题思路 第一次考场上面写三分,然而我并不知道三分无法处理不是严格单峰的情况,但凡有一个平台都不 ...
- Windows内核中的CPU架构-7-陷阱门(32-Bit Trap Gate)
Windows内核中的CPU架构-7-陷阱门(32-Bit Trap Gate) 陷阱门和中断门几乎是一模一样的: (注:图里高32位中的第11位的值为D,其实是1) 除了高32位中的type字段的内 ...
- RabbitMQ保证消息的顺序性
当我们的系统中引入了MQ之后,不得不考虑的一个问题是如何保证消息的顺序性,这是一个至关重要的事情,如果顺序错乱了,就会导致数据的不一致. 比如:业务场景是这样的:我们需要根据mysql的b ...
- RocketMQ源码详解 | Broker篇 · 其四:事务消息、批量消息、延迟消息
概述 在上文中,我们讨论了消费者对于消息拉取的实现,对于 RocketMQ 这个黑盒的心脏部分,我们顺着消息的发送流程已经将其剖析了大半部分.本章我们不妨乘胜追击,接着讨论各种不同的消息的原理与实现. ...
- [bzoj1711]吃饭
由于无法直接将果汁和饮料连边,所以将人放在中间,果汁和饮料放在两侧,然后分别向对应的人连边.同时,为了保证每一个人只被算一次,对每一个人裂点,两个点中间连一条流量为1的边. 1 #include< ...
- 多声部处理细节之crossstaff音符处理
下面给出这几个案例的代码 ,可直接粘贴编译. \version "2.20.0" \language "english" \paper { #(set-pape ...
- Jenkins系列-权限管理
在实际工作中,存在多个团队都需要Jenkins来实现持续交付,但是又希望不同团队之间进行隔离,每个项目有自己的view, 只能看到自己项目的jenkins job. 但是,jenkins默认的权限管理 ...