Predict the output of following C++ programs.

Question 1

 1 #include <iostream>
2 using namespace std;
3
4 int fun(int a, int b = 1, int c =2)
5 {
6 return (a + b + c);
7 }
8
9 int main()
10 {
11 cout << fun(12, ,2);
12 return 0;
13 }

  Output: Compiler Error in function call fun(12, ,2)
  With default arguments, we cannot skip an argument in the middle. Once an argument is skipped, all the following arguments must be skipped. The calls fun(12) and fun(12, 2) are valid.

Question 2

 1 #include<iostream>
2 using namespace std;
3
4 /* local variable is same as a member's name */
5 class Test
6 {
7 private:
8 int x;
9 public:
10 void setX (int x) { Test::x = x; }
11 void print() { cout << "x = " << x << endl; }
12 };
13
14 int main()
15 {
16 Test obj;
17 int x = 40;
18 obj.setX(x);
19 obj.print();
20 return 0;
21 }

  Output:

  x = 40
  Scope resolution operator can always be used to access a class member when it is made hidden by local variables. So the line “Test::x = x” is same as “this->x = x”

Question 3

 1 #include<iostream>
2 using namespace std;
3
4 class Test
5 {
6 private:
7 int x;
8 static int count;
9 public:
10 Test(int i = 0) : x(i)
11 {
12 }
13 Test(const Test& rhs) : x(rhs.x)
14 {
15 ++count;
16 }
17 static int getCount()
18 {
19 return count;
20 }
21 };
22
23 int Test::count = 0;
24
25 Test fun()
26 {
27 return Test();
28 }
29
30 int main()
31 {
32 Test a = fun();
33 cout<< Test::getCount();
34 return 0;
35 }

  Output: Compiler Dependent
  The line “Test a = fun()” may or may not call copy constructor. So output may be 0 or 1. If copy elision happens in your compiler, the copy constructor will not be called. If copy elision doesn’t happen, copy constructor will be called. The gcc compiler produced the output as 0.

  

  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  16:08:17

Output of C++ Program | Set 12的更多相关文章

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

随机推荐

  1. 流媒体技术的应用,如何搭建一个SimpleNVR流媒体服务系统

    Onvif/RTSP流媒体服务 SimpleNVR Onvif/RTSP流媒体服务是一款软硬一体音视频流媒体服务软件.它是在5G.AI.云计算.大数据.物联网等网络技术大规模商用后,用户要求视频随时随 ...

  2. Qt 项目管理文件(.pro) 详解

    项目文件目录树 在 Qt Creator 中新建一个 Widget Application 项目 samp2_1,在选择窗口基类的页面选择 QWidget 作为窗体基类,并选中"Genera ...

  3. Linux常用命令和快捷键整理:(2)常用快捷键

    前言: Linux常用快捷键和基本命令整理,先上思维导图: linux常用命令请见:https://www.cnblogs.com/yinzuopu/p/15516499.html 基本快捷键的使用 ...

  4. easypoi导出动态表头excel

    easypoi导出动态表头excel 1: springBoot项目maven依赖: <dependency> <groupId>cn.afterturn</groupI ...

  5. Nginx支持WebSocket反向代理

    WebSocket是目前比较成熟的技术了,WebSocket协议为创建客户端和服务器端需要实时双向通讯的webapp提供了一个选择.其为HTML5的一部分,WebSocket相较于原来开发这类app的 ...

  6. python有关于图像的深度和通道

    目录: (一)图像的深度和图像的通道  (1)图像的深度  (2)图像的通道 (二)自定义一张多通道的图片 (1)zeros 函数 (2)ones  函数 (三)自定义一张单通道的图片 (四)像素操作 ...

  7. [atAGC045F]Division into Multiples

    令$d=\gcd(a,b)$,可以发现$c|(ax+by)$等价于$lcm(c,d)|(ax+by)$,因此不妨令$c'=lcm(c,d)$,然后将$a$.$b$和$c$同时除以$d$ 接下来设$(a ...

  8. [luogu4331]数字序列

    令$a'_{i}=a_{i}+n-i$.$b'_{i}=b_{i}+n-i$,代价仍然是$\sum_{i=1}^{n}|a'_{i}-b'_{i}|$,但条件变为了$b'_{i}\le b'_{i+1 ...

  9. 多线程07.thread-join

    package com.wangwenjun.concurrency.chapter5; public class ThreadJoin3 { public static void main(Stri ...

  10. 2017Java前景怎么样?

    当今社会互联网软件行业属于高薪技术行业,伴随着互联网的发展Java在Web领域的优势也日渐凸显,并且java语言本身就应用最广泛,最高效.据说,全球有25亿Java器件运行着Java,450多万Jav ...