C++程序输出5

难度等级:新手 预测下面的C++程序的输出。 问题1

null

C++

#include<iostream>
using namespace std;
class Test {
int value;
public :
Test( int v);
};
Test::Test( int v) {
value = v;
}
int main() {
Test t[100];
return 0;
}


输出:

Compiler error

类测试有一个用户定义的构造函数“Test(intv)”,它需要一个参数。它没有没有没有任何参数的构造函数,因为如果用户定义了构造函数,编译器不会创建默认构造函数(请参见 ).以下修改的程序工作正常,没有任何错误。

C++

#include<iostream>
using namespace std;
class Test {
int value;
public :
Test( int v = 0);
};
Test::Test( int v) {
value = v;
}
int main() {
Test t[100];
return 0;
}


问题2

C++

#include<iostream>
using namespace std;
int &fun() {
static int a = 10;
return a;
}
int main() {
int &y = fun();
y = y +30;
cout<<fun();
return 0;
}


输出:

40

程序运行良好,因为“a”是静态的。因为“a”是静态的,所以即使在fun()返回后,它的内存位置仍然有效。因此可以返回对静态变量的引用。 问题3

C++

#include<iostream>
using namespace std;
class Test
{
public :
Test();
};
Test::Test()  {
cout<< "Constructor Called " ;
}
int main()
{
cout<< "Start " ;
Test t1();
cout<< "End " ;
return 0;
}


输出:

StartEnd

请注意,行“Test t1();”不是构造函数调用。编译器将这一行视为函数t1的声明,该函数不接收任何参数,并返回类型为Test的对象。 如果您发现任何答案/解释不正确,或者您想分享有关上述主题的更多信息,请发表评论

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