c构造函数和析构函数_C ++构造函数,析构函数能力问题和答案(第2组)
c構(gòu)造函數(shù)和析構(gòu)函數(shù)
C ++構(gòu)造函數(shù)和析構(gòu)函數(shù)能力問題列表 (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)如果我們不創(chuàng)建自己的構(gòu)造函數(shù),會(huì)隨類自動(dòng)添加構(gòu)造函數(shù)?
Default Constructor
默認(rèn)構(gòu)造函數(shù)
Copy Constructor
復(fù)制構(gòu)造函數(shù)
Both default and copy constructors
默認(rèn)構(gòu)造函數(shù)和復(fù)制構(gòu)造函數(shù)
None
沒有
Correct Answer: 3
正確答案:3
Both default and copy constructors
默認(rèn)構(gòu)造函數(shù)和復(fù)制構(gòu)造函數(shù)
In C++ class implementation, if we do not create any constructor, the default and copy constructors are added automatically to the class.
在C ++類實(shí)現(xiàn)中,如果我們不創(chuàng)建任何構(gòu)造函數(shù),則默認(rèn)構(gòu)造函數(shù)和復(fù)制構(gòu)造函數(shù)會(huì)自動(dòng)添加到該類中。
2) Does assignment operator implement automatically with the class?
2)賦值運(yùn)算符是否與類一起自動(dòng)實(shí)現(xiàn)?
Yes
是
No
沒有
Correct Answer - 2
正確答案-2
Yes
是
If we do not implement the assignment operator, it automatically added to the class.
如果我們不實(shí)現(xiàn)賦值運(yùn)算符,它將自動(dòng)添加到類中。
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; }Constructor called
構(gòu)造函數(shù)稱為
Program successfully executed – no output
程序成功執(zhí)行-無(wú)輸出
Compile time error
編譯時(shí)間錯(cuò)誤
Run time error
運(yùn)行時(shí)錯(cuò)誤
Correct Answer - 3
正確答案-3
Compile time error
編譯時(shí)間錯(cuò)誤
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.
在類定義中,沒有指定訪問修飾符,因此(按照標(biāo)準(zhǔn))所有成員函數(shù)和數(shù)據(jù)成員默認(rèn)情況下都是私有的。 并且,構(gòu)造函數(shù)不能為私有。
This will be the output
這將是輸出
main.cpp:6:5: error: 'Example::Example()' is privateExample() {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; }Constructor called
構(gòu)造函數(shù)稱為
Constructor called Constructor called
構(gòu)造函數(shù)稱為構(gòu)造函數(shù)稱為
Compile time error
編譯時(shí)間錯(cuò)誤
Run time error
運(yùn)行時(shí)錯(cuò)誤
Correct Answer - 2
正確答案-2
Constructor called Constructor called
構(gòu)造函數(shù)稱為構(gòu)造函數(shù)稱為
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".
在類定義中,構(gòu)造函數(shù)是公共的,因此沒有任何編譯時(shí)或運(yùn)行時(shí)錯(cuò)誤。 我們正在創(chuàng)建“ Example”類的兩個(gè)對(duì)象“ Ex1”和“ Ex2”; 構(gòu)造函數(shù)將被調(diào)用兩次。 因此,輸出將為“稱為構(gòu)造函數(shù)的構(gòu)造函數(shù),稱為” 。
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; }Compile time error
編譯時(shí)間錯(cuò)誤
Run time error
運(yùn)行時(shí)錯(cuò)誤
a = 10, b = 20
a = 10,b = 20
a = 10, b = 10
a = 10,b = 10
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.
像結(jié)構(gòu)一樣,我們可以像下面的示例一樣初始化類的公共數(shù)據(jù)成員(使用初始化器列表) Ex1 = {10,20}; 因此,將10分配給a ,將20分配給b 。 因此,輸出將為a = 10,b = 20 。
? Set 1 ?設(shè)置1 Set 3 ?設(shè)置3?翻譯自: https://www.includehelp.com/cpp-programming/constructor-and-destructor-aptitudue-questions-and-answers-2.aspx
c構(gòu)造函數(shù)和析構(gòu)函數(shù)
總結(jié)
以上是生活随笔為你收集整理的c构造函数和析构函数_C ++构造函数,析构函数能力问题和答案(第2组)的全部?jī)?nèi)容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: kotlin获取属性_Kotlin程序|
- 下一篇: 漫画:给女朋友介绍什么是 “元宇宙” ?