首页 技术 正文
技术 2022年11月8日
0 收藏 685 点赞 2,001 浏览 1569 个字

  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

相关推荐
python开发_常用的python模块及安装方法
adodb:我们领导推荐的数据库连接组件bsddb3:BerkeleyDB的连接组件Cheetah-1.0:我比较喜欢这个版本的cheeta…
日期:2022-11-24 点赞:878 阅读:9,078
Educational Codeforces Round 11 C. Hard Process 二分
C. Hard Process题目连接:http://www.codeforces.com/contest/660/problem/CDes…
日期:2022-11-24 点赞:807 阅读:5,553
下载Ubuntn 17.04 内核源代码
zengkefu@server1:/usr/src$ uname -aLinux server1 4.10.0-19-generic #21…
日期:2022-11-24 点赞:569 阅读:6,402
可用Active Desktop Calendar V7.86 注册码序列号
可用Active Desktop Calendar V7.86 注册码序列号Name: www.greendown.cn Code: &nb…
日期:2022-11-24 点赞:733 阅读:6,177
Android调用系统相机、自定义相机、处理大图片
Android调用系统相机和自定义相机实例本博文主要是介绍了android上使用相机进行拍照并显示的两种方式,并且由于涉及到要把拍到的照片显…
日期:2022-11-24 点赞:512 阅读:7,814
Struts的使用
一、Struts2的获取  Struts的官方网站为:http://struts.apache.org/  下载完Struts2的jar包,…
日期:2022-11-24 点赞:671 阅读:4,898