C++程序输出12

预测下列C++程序的输出。

null

问题1

#include <iostream>
using namespace std;
int fun( int a, int b  = 1, int c =2)
{
return (a + b + c);
}
int main()
{
cout << fun(12, ,2);
return 0;
}


输出:函数调用中的编译器错误(12,2) 使用默认参数,我们不能跳过中间的一个参数。跳过参数后,必须跳过以下所有参数。fun(12)和fun(12,2)是有效的。

问题2

#include<iostream>
using namespace std;
/* local variable is same as a member's name */
class Test
{
private :
int x;
public :
void setX ( int x) { Test::x = x; }
void print() { cout << "x = " << x << endl; }
};
int main()
{
Test obj;
int x = 40;
obj.setX(x);
obj.print();
return 0;
}


输出:

x = 40

当类成员被局部变量隐藏时,作用域解析运算符始终可用于访问该类成员。因此,行“Test::x=x”与“this->x=x”相同

问题3

#include<iostream>
using namespace std;
class Test
{
private :
int x;
static int count;
public :
Test( int i = 0) : x(i) {}
Test( const Test& rhs) : x(rhs.x) { ++count;  }
static int getCount() { return count; }
};
int Test::count = 0;
Test fun()
{
return Test();
}
int main()
{
Test a = fun();
cout<< Test::getCount();
return 0;
}


输出:依赖于编译器 行“Test a=fun()”可能调用也可能不调用复制构造函数。所以输出可能是0或1。如果 复制省略 在编译器中发生时,不会调用复制构造函数。如果复制省略没有发生,将调用复制构造函数。gcc编译器生成的输出为0。

如果您发现任何答案/解释不正确,或者您想分享有关上述主题的更多信息,请发表评论。

© 版权声明
THE END
喜欢就支持一下吧
点赞8 分享