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. Java Logback简易教程

    本作品采用知识共享署名-非商业性使用 4.0 国际许可协议进行许可. 一.前言 本文以一个简单的项目为例,一步步展示logback的同步和异步配置方法,并且配置的日志要求满足阿里巴巴Java开发手册- ...

  2. 【java+selenium3】时间控件 (九)

    1.问题描述: 在应用selenium实现web自动化时,经常会遇到处理日期控件点击问题,手工很简单,可以一个个点击日期控件选择需要的日期,但自动化执行过程中,完全复制手工这样的操作就有点难了. 如图 ...

  3. APP 自动化之手势操作appium提供API详解(四)

    一.手势操作1.上下左右滑屏 swipe---滑动 java-client 4.x 是有swipe方法的,可以通过传递坐标信息就可以完成滑动androidDriver.swipe(startx, st ...

  4. ucharts tooltip弹窗自定义换行

    这个东西吧,也许是因为菜,看了3小时,下面给出解决方案 1. 找到源码下面的这个文件 2. 增加绿色方框中的代码 3.组件调用的时候有一个opts属性 :opts="{ extra: { t ...

  5. 96.n-1位数

    描述 已知w是一个大于10但不大于1000000的无符号整数,若w是n(n≥2)位的整数,则求出w的后n-1位的数. 输入 第一行为M,表示测试数据组数. 接下来M行,每行包含一个测试数据. 输出 输 ...

  6. halcon语法讲解

    前言 最近换工作,在学习了解halcon工具,每天总结分析今天所学知识,今天是基础语法篇! 1.基本语法 描述 语法 等号 := 不等号 # 注释符 * 字符串赋值 str:='halcon' 等于比 ...

  7. soname and real name

    [1] https://wiki.openssl.org/index.php/OpenSSL_1.1.0_Changes#Backward_compatibility [2] https://akka ...

  8. 第04章_MySQL运算符详解

    第04章_运算符 1. 算术运算符 算术运算符主要用于数学运算,其可以连接运算符前后的两个数值或表达式,对数值或表达式进行加(+).减(-).乘(*).除(/)和取模(%)运算. 1.加法与减法运算符 ...

  9. 一个没被spring管理的类怎么创建对象并使用里面的方法

    一个对象new出来的,如果不是构造器注入@Data 也不好使啊,尝试构造器注入一下,或者set进去 第二次尝试使用这个. 向这种只能构造器注入或者通过上面的set方法来注入了,component是不好 ...

  10. 『学了就忘』Linux权限管理 — 54、sudo授权

    目录 1.什么是sudo授权 2. sudo授权说明 3.sudo命令的使用 示例1 示例2 示例3 1.什么是sudo授权 在Linux系统中,/sbin/和/usr/sbin/两个目录中的命令只有 ...