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

« Set 1 «设置1 Set 3 »设置3»

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

c构造函数和析构函数

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

  1. c构造函数和析构函数_C ++构造函数和析构函数| 查找输出程序| 套装1

    c构造函数和析构函数 Program 1: 程序1: #include <iostream> using namespace std; class Sample { private: in ...

  2. c构造函数和析构函数_C ++构造函数和析构函数| 查找输出程序| 套装2

    c构造函数和析构函数 Program 1: 程序1: #include<iostream> using namespace std; class Sample { private: int ...

  3. c构造函数和析构函数_C ++构造函数和析构函数| 查找输出程序| 套装3

    c构造函数和析构函数 Program 1: 程序1: #include <iostream> using namespace std; class Sample { private: in ...

  4. c++单链表【构造函数、运算符重载、析构函数、增删查改等】

    c++中的单向链表写法:实现增删查改.构造函数.运算符重载.析构函数等. 建立头文件SList.h #pragma oncetypedef int DataType; //SList要访问SListN ...

  5. C ++ 类 | 类的例子,构造函数(Constructors),析构函数(Destructors)_2

    目录 类的例子 构造函数(Constructors) 析构函数(Destructors) 类的例子 在这个程序中创建一个名为Cats的类.它有三个私人成员:姓名.品种.年龄.创建所有的集合和获取函数 ...

  6. C++11六大函数(构造函数,移动构造函数,移动赋值操作符,复制构造函数,赋值操作符,析构函数)

    转载自:http://blog.csdn.net/jofranks/article/details/17438955 版权声明:本文为博主原创文章,未经博主允许不得转载. 在C++中,有三大函数复制控 ...

  7. 一个程序让你学会C++构造函数与重载构造、析构函数【C++类的经典使用案例】

    文章目录 一.构造函数的用法 二.构造函数的重载 三.析构函数 一.构造函数的用法 #include <iostream> using namespace std;//声明Time类 cl ...

  8. C++析构函数与构造函数深拷贝浅拷贝(C++初学面向对象编程)

    文章目录 一.析构函数 二.C++默认生成的函数 三.构造与析构的调用顺序 四.构造函数的浅拷贝 五.构造函数的深拷贝 一.析构函数 1.析构函数的作用 对象消亡时,自动被调用,用来释放对象占用的内存 ...

  9. c/c++教程 - 2.4.2.1~2 对象的初始化和清理,构造函数和析构函数,构造函数的分类和调用(有参构造,无参构造,普通构造,拷贝构造,括号法,显示法,隐式转换法,匿名对象)

    目录 4.2 对象的初始化和清理 4.2.1 构造函数和析构函数 4.2.2 构造函数的分类及调用 相关教程 4.2 对象的初始化和清理 生活中我们买的电子产品都基本会有出厂设置,在某一天我们不用时候 ...

最新文章

  1. 关于 ES6 的 let ,var和 const
  2. SAP Fiori Elements - When is catalog annotation $value loaded
  3. 7-3 作业调度算法--高响应比优先 (40 分)(思路+详解+vector容器做法)Come Baby!!!!!
  4. 杨振宁讲(经典)数学笑话兼论数学和物理的关系
  5. 为什么微软账号被暂时停用_微软向Win10 20H2推出测试版更新KB4586853修复多种已知问题...
  6. Android 系统(220)---如何快速对系统重启问题进行归类
  7. 多益网络 2016 春季实习校招笔试回顾(C++游戏后台)
  8. sweetalert2使用教程
  9. 麟龙指标通达信指标公式源码_麟龙指标通达信指标公式源码
  10. Docker安装(有网环境下) 最新版docker-ce安装教程
  11. 使用 SOUI 开发高 DPI 桌面应用程序
  12. 【按键精灵】N秒内找图函数封装
  13. idea中java程序打jar包的两种方式(超详细)
  14. 食品巨头布勒宣布部署区块链以对抗沙门氏菌和大肠杆菌
  15. openjweb1.8 java web应用快速开发平台产品白皮书
  16. 一种与生活周旋的能力
  17. 2020 - 04 - 11 个人笔记
  18. 2022中式烹调师(初级)考试题及在线模拟考试
  19. 【Unity】VideoPlayer实现视频播放
  20. 【报告分享】 2020年中国互联网医疗研究报告-36kr(附下载)

热门文章

  1. php-fpm初始化失败,FPM的初始化 - [ PHP7的内核剖析 ] - 在线原生手册 - php中文网
  2. Python报错:PermissionError: [Errno 13] Permission denied 解决方案详解
  3. innobackupex远程备份脚本
  4. Angular Chart.js第三方库ng-chartjs基础使用
  5. Hadoop生态圈-Ambari控制台功能简介
  6. JavaWeb网上图书商城完整项目--day02-14.登录功能的login页面处理
  7. Lync Server 2010 安装部署系列三:添加DNS记录
  8. Delphi利用Windows GDI实现文字倾斜
  9. kindeditor图片批量上传失败问题
  10. docker rabbitmq_一文看懂Rabbitmq,从安装到实战演练