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

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

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

c构造函数和析构函数

本文来自互联网用户投稿,该文观点仅代表作者本人,不代表本站立场。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如若转载,请注明出处:http://www.mzph.cn/news/546090.shtml

如若内容造成侵权/违法违规/事实不符,请联系多彩编程网进行投诉反馈email:809451989@qq.com,一经查实,立即删除!

相关文章

看故事学知识,这篇Java代理的文章妙啊!

这是我的第 208 期分享作者 | java金融来源 | java金融&#xff08;ID&#xff1a;java4299&#xff09;分享 | Java中文社群&#xff08;ID&#xff1a;javacn666&#xff09;什么是代理代理模式是常用的java设计模式&#xff0c;他的特征是代理类与委托类有同样的接口&#x…

[下载]青岛交通旅游地图[download]

清闲无事 所以就把青岛地图扫描了一下有需要青岛旅游地图的可以下载the download 【下载】

Java Thread类的静态void sleep(long time_in_ms)方法,带示例

线程类静态无效睡眠(long time_in_ms) (Thread Class static void sleep(long time_in_ms)) This method is available in package java.lang.Thread.sleep(long time_in_ms). 软件包java.lang.Thread.sleep(long time_in_ms)中提供了此方法。 sleep(long time_in_ms) method i…

阿里《Java开发手册》中的 1 个bug!

这是我的第 210 期分享作者 | 王磊来源 | Java中文社群&#xff08;ID&#xff1a;javacn666&#xff09;转载请联系授权&#xff08;微信ID&#xff1a;GG_Stone&#xff09;本来打算写一篇《阿里巴巴为什么不允许日志输出时&#xff0c;使用字符串拼接&#xff1f;》的文章&a…

zoj 1006 do the untwist

题目见zoj 1006 或poj 1317 简单的解密算法&#xff0c;直接套用题目中公式即可。 /* zoj 1006 Do the Untwist */ #include <stdio.h> #include <string.h>#define MAXLEN 80 #define MAGICNUM 28char num2Char(int n); int char2Num(char c); int main(void)…

安装完SqlServer2008,wamp服务器无法启动的问题

"开始"->"程序"->Microsoft SQL Server 2008->配置工具->SQL Server配置管理器->SQL Server服务: 只保留SQL Server(MSSQLSERVER)(正在运行)&#xff0c;其他的全部设为停止。 重启wamp服务器成功!

猫版超级玛丽 附下载

不再多言 玩者自知しょぼんのアクション猫版超级玛丽 下载

驳《阿里「Java开发手册」中的1个bug》?

这是我的第 211 期分享作者 | 王磊来源 | Java中文社群&#xff08;ID&#xff1a;javacn666&#xff09;转载请联系授权&#xff08;微信ID&#xff1a;GG_Stone&#xff09;前两天写了一篇关于《阿里Java开发手册中的 1 个bug》的文章&#xff0c;评论区有点炸锅了&#xff0…

Java String indexOf(String substr,int fromIndex)方法,带示例

字符串indexOf(String substr&#xff0c;int fromIndex)方法 (String indexOf(String substr, int fromIndex) Method) indexOf(String substr, int fromIndex) is a String method in Java and it is used to get the index of a specified substring in the string from giv…

zoj 1078 palindrom numbers

题目见zoj 1078 主要是判断一个整数在基数为2-16之间的某个数字时是否为回文&#xff0c;我是直接该整数转换成对应基数的表示的逆序列&#xff0c;并计算出该表示下的值&#xff0c;判断是否等于这个整数值&#xff0c;如果相等&#xff0c;那么就是回文&#xff0c;如果不相…

iredmail邮件服务器之修改默认的web服务端口号

安装iredmail之后&#xff0c;由于需要在路由器上做端口映射以便在外网访问webmail&#xff0c;因此端口不能和WEB服务的端口好冲突&#xff0c;所以需要修改邮件服务器的httpd服务的端口。 一、apache/httpd的http服务和https服务端口号都要修改。 基本服务端口好办&#xff0…

轻松学算法的秘密!可视化算法网站汇总!(附动图)

对于「算法」的第一印象&#xff0c;我相信大部分人都是一样的&#xff0c;就是一个“难”字了得。而我比较特殊&#xff0c;我的第一印象、第二印象以至第 N 印象都觉得很难&#xff0c;所以为了更好的学习和理解算法&#xff0c;我千金一掷一下买了一堆的算法书&#xff0c;有…

从100套真题中提炼而出的100个经典句子

1. Typical of the grassland dwellers of the continent is the American antelope, or pronghorn. 1.美洲羚羊&#xff0c;或称叉角羚&#xff0c;是该大陆典型的草原动物。 2. Of the millions who saw Haley’s comet in 1986, how many people will live long enough to s…

字符串大写转小写库函数_PHP程序无需使用库函数即可将字符串转换为大写

字符串大写转小写库函数Given a string and we have to convert it into uppercase string without using any library function. 给定一个字符串&#xff0c;我们必须在不使用任何库函数的情况下将其转换为大写字符串。 PHP code: PHP代码&#xff1a; <?php//function …

zoj 1091 Knight Moves

题目见zoj 1091 使用宽度搜索优先来求解&#xff0c;这个算法已经忘记的差不多了&#xff0c;所以写出来的代码很罗嗦&#xff0c;看起来很不清晰。 好像还可以直接用公式或者神经网络算法求解,详见Knights Tour /* zoj 1091 Knight Moves */ #include <stdio.h> #incl…

1.基本类型的指针

例子一&#xff1a; #include<stdio.h>int main() {int i10;int * p&i; //p是个变量名字&#xff0c;int * 表示该p变量只能存储int类型变量的地址。//int *p&i;等价于 int *p; p&i;//p存放了i的地址,p指向i,*p就是i变量本身printf("%d\n",p);// …

VB中KeyCode常数用法 VB 按键

VB中KeyCode常数用法可在代码中的任何地方用下列常数代替实际值&#xff1a;常数 值 描述vbKeyLButton 0x1 鼠标左键vbKeyRButton 0x2 鼠标右键vbKeyCancel 0x3 CANCEL 键vbKeyMButton 0x4 鼠标中键vbKeyBack 0x8 BACKSPACE 键vbKeyTab 0x9 TAB 键vbKeyClear 0xC CLEAR 键vbKey…

zoj 1088 System Overload

约瑟夫环 (josephus problem &#xff09;问题&#xff0c;有公式 可以直接套用 我使用暴力破解方法求解&#xff08;用时3秒多&#xff09;。 代码如下&#xff1a; /* zoj 1088 System Overload */ #include <stdio.h> #include <string.h>#define MAXBUILDING …

链表竟然比数组慢了1000多倍?(动图+性能评测)

这是我的第 215 期分享作者 | 王磊来源 | Java中文社群&#xff08;ID&#xff1a;javacn666&#xff09;转载请联系授权&#xff08;微信ID&#xff1a;GG_Stone&#xff09;数组和链表是程序中常用的两种数据结构&#xff0c;也是面试中常考的面试题之一。然而对于很多人来说…