C ++构造函数和析构函数能力问题列表 (List of C++ Constructor and Destructor Aptitude Questions & Answers)
1) Can we define a class without creating constructors?
Answer
Correct Answer - 1
Yes
Yes, we can define a class without creating constructors.
2) Which is the correct form of default constructor for following class?
#include <iostream>
using namespace std;
class sample
{
private:
int x,y;
};
Answer
Correct Answer - 4
Both 1 and 2
2)以下类的默认构造函数的正确形式是什么?
回答
正确答案-4
1和2
3) What will be the output of following program?
#include <iostream>
using namespace std;
class sample {
private:
int x, y;
public:
sample(int a, int b)
{
x = a;
y = b;
}
};
int main()
{
sample s;
return 0;
}
Answer
Correct Answer - 1
Compile Time Error
There is no default constructor in this class definition, and we cannot create an object like this, the correct form of creating object is : sample s(10,20); - pass two values because parameterized constructor is exists in the class definition.
3)以下程序的输出是什么?
回答
正确答案-1
编译时错误
这个类定义中没有默认的构造函数,我们不能创建这样的对象,创建对象的正确形式是:sample s(10,20); -传递两个值,因为类定义中存在参数化构造函数。
4) What will be the output of following program?
#include <iostream>
using namespace std;
class sample {
private:
int x;
public:
sample()
{
x = 0;
printf("Object created.");
}
sample(int a) { x = a; }
};
int main()
{
sample s;
return 0;
}
Answer
Correct Answer - 2
Object Created.