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的更多相关文章

  1. Output of C++ Program | Set 18

    Predict the output of following C++ programs. Question 1 1 #include <iostream> 2 using namespa ...

  2. Output of C++ Program | Set 17

    Predict the output of following C++ programs. Question 1 1 #include <iostream> 2 using namespa ...

  3. Output of C++ Program | Set 16

    Predict the output of following C++ programs. Question 1 1 #include<iostream> 2 using namespac ...

  4. Output of C++ Program | Set 15

    Predict the output of following C++ programs. Question 1 1 #include <iostream> 2 using namespa ...

  5. Output of C++ Program | Set 14

    Predict the output of following C++ program. Difficulty Level: Rookie Question 1 1 #include <iost ...

  6. Output of C++ Program | Set 13

    Predict the output of following C++ program. 1 #include<iostream> 2 using namespace std; 3 4 c ...

  7. Output of C++ Program | Set 11

    Predict the output of following C++ programs. Question 1 1 #include<iostream> 2 using namespac ...

  8. Output of C++ Program | Set 9

    Predict the output of following C++ programs. Question 1 1 template <class S, class T> class P ...

  9. 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 ...

  10. Output of C++ Program | Set 6

    Predict the output of below C++ programs. Question 1 1 #include<iostream> 2 3 using namespace ...

随机推荐

  1. spring security之 默认登录页源码跟踪

    spring security之 默认登录页源码跟踪 ​ 2021年的最后2个月,立个flag,要把Spring Security和Spring Security OAuth2的应用及主流程源码研究透 ...

  2. 攻防世界 Misc 新手练习区 gif Writeup

    攻防世界 Misc 新手练习区 gif Writeup 题目介绍 题目考点 仔细联想 字符转换 Writeup 下载附件并打开 104张黑白图 发现是一堆黑色和白色的图片,按某种规律排列,猜想flag ...

  3. Unity——技能系统(二)

    Unity技能系统(二) Unity技能系统(一) Demo展示: 五.技能管理和释放 1.CharacterSkillSystem 技能系统类,给外部(技能按钮,按键)提供技能释放方法: 技能释放逻 ...

  4. Part 35 AngularJS caseInsensitiveMatch and Inline Templates

    In this video we will discuss 2 simple but useful features in Angular caseInsensitiveMatch Inline Te ...

  5. PAT A1107——并查集

     Social Clusters When register on a social network, you are always asked to specify your hobbies in ...

  6. [cf582E]Boolean Function

    由于每一个运算都有括号,因此添加的运算不会改变运算顺序 先将其建出一棵表达式树,也就是维护两个栈,是节点和运算符优先级单调递增的栈(设置左括号优先级最低,右括号弹出直至左括号) 每一次运算,也就是新建 ...

  7. [bzoj5417]你的名字

    先考虑l=1,r=|s|的部分分,需要求出t每一个前缀的不是s子串的最长后缀,记作pp[k],有以下限制:1.pp[pos[k]]<len(pos[k]表示k的某一个结束位置),因为不能被匹配 ...

  8. Kubernetes(K8s)极速入门

    1. 概述 老话说的好:努力学习,努力提高,做一个有真才实学的人. 言归正传,之前我们聊了 如何使用国内的镜像源搭建 kubernetes(k8s)集群 ,今天我们来聊聊如何在 kubernetes( ...

  9. 【Microsoft Azure 的1024种玩法】八. 基于Azure云端轻松打造一款好用的私有云笔记

    [简介] Leanote一款开源云笔记软件,它使用Go的Web框架revel和MongoDB开发完成的,其是目前为止发现的最有bigger的云笔记,它支持markdown输入,代码高亮,多人协作,笔记 ...

  10. System类的常用方法(currentTimeMillis与arraycopy)

    System类的常用方法 currentTimeMillis与arraycopy import java.util.Arrays; /* java.lang.System类中提供了大量的静态方法,可以 ...