c构造函数和析构函数_C ++构造函数,析构函数能力问题和答案(第2组)

c构造函数和析构函数

C ++构造函数和析构函数能力问题列表 (List of C++ Constructor and Destructor Aptitude Questions & Answers)

1) Constructor(s) which is/are added automatically with a class, if we do not create our own constructor?

1)如果我们不创建自己的构造函数,会随类自动添加构造函数?

  1. Default Constructor

    默认构造函数

  2. Copy Constructor

    复制构造函数

  3. Both default and copy constructors

    默认构造函数和复制构造函数

  4. None

    没有

Answer & Explanation 答案与解释

Correct Answer: 3

正确答案:3

Both default and copy constructors

默认构造函数和复制构造函数

In C++ class implementation, if we do not create any constructor, the default and copy constructors are added automatically to the class.

在C ++类实现中,如果我们不创建任何构造函数,则默认构造函数和复制构造函数会自动添加到该类中。

2) Does assignment operator implement automatically with the class?

2)赋值运算符是否与类一起自动实现?

  1. Yes

  2. No

    没有

Answer & Explanation 答案与解释

Correct Answer - 2

正确答案-2

Yes

If we do not implement the assignment operator, it automatically added to the class.

如果我们不实现赋值运算符,它将自动添加到类中。

3) What will be the output of the following code?

3)以下代码的输出是什么?

#include<iostream>
using namespace std;

//class definition
class Example {
    Example() { 
        cout << "Constructor called"; 
    }
};
 
//main() code 
int main()
{
   Example Ex;
   return 0;
}

  1. Constructor called

    构造函数称为

  2. Program successfully executed – no output

    程序成功执行-无输出

  3. Compile time error

    编译时间错误

  4. Run time error

    运行时错误

Answer & Explanation 答案与解释

Correct Answer - 3

正确答案-3

Compile time error

编译时间错误

In the class definition, there is no access modifier is specified, thus (as per the standard) all member functions and data members are private by default. And, the constructor cannot be a private.

在类定义中,没有指定访问修饰符,因此(按照标准)所有成员函数和数据成员默认情况下都是私有的。 并且,构造函数不能为私有。

This will be the output

这将是输出

main.cpp:6:5: error: 'Example::Example()' is private
     Example() {

4) What will be the output of the following code?

4)以下代码的输出是什么?

#include <iostream>
using namespace std;

//class definition
class Example {
public:
    Example()
    {
        cout << "Constructor called ";
    }
};

//main() code
int main()
{
    Example Ex1, Ex2;
    return 0;
}

  1. Constructor called

    构造函数称为

  2. Constructor called Constructor called

    构造函数称为构造函数称为

  3. Compile time error

    编译时间错误

  4. Run time error

    运行时错误

Answer & Explanation 答案与解释

Correct Answer - 2

正确答案-2

Constructor called Constructor called

构造函数称为构造函数称为

In the class definition, the constructor is public, so there is no any compile time or run time error. We are creating two objects “Ex1” and “Ex2” of “Example” class; constructor will be called two times. Thus, the output will be "Constructor called Constructor called".

在类定义中,构造函数是公共的,因此没有任何编译时或运行时错误。 我们正在创建“ Example”类的两个对象“ Ex1”和“ Ex2”; 构造函数将被调用两次。 因此,输出将为“称为构造函数的构造函数,称为”

5) What will be the output of the following code?

5)以下代码的输出是什么?

#include <iostream>
using namespace std;

//class definition
class Example {
public:
    int a;
    int b;
};

//main() code
int main()
{
    Example Ex1 = { 10, 20 };
    cout << "a = " << Ex1.a << ", b = " << Ex1.b;
    return 0;
}

  1. Compile time error

    编译时间错误

  2. Run time error

    运行时错误

  3. a = 10, b = 20

    a = 10,b = 20

  4. a = 10, b = 10

    a = 10,b = 10

Answer & Explanation 答案与解释

Correct Answer - 3

正确答案-3

a = 10, b = 20

a = 10,b = 20

Like structures, we can initialize class's public data members (using initializer list) like this Example Ex1 = {10, 20}; so, 10 will be assigned to a and 20 will be assigned to b. Thus, the output will be a = 10, b = 20.

像结构一样,我们可以像下面的示例一样初始化类的公共数据成员(使用初始化器列表) Ex1 = {10,20}; 因此,将10分配给a ,将20分配给b 。 因此,输出将为a = 10,b = 20

翻译自: https://www.includehelp.com/cpp-programming/constructor-and-destructor-aptitudue-questions-and-answers-2.aspx

c构造函数和析构函数

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值