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)如果我們不創建自己的構造函數,會隨類自動添加構造函數?
Default Constructor
默認構造函數
Copy Constructor
復制構造函數
Both default and copy constructors
默認構造函數和復制構造函數
None
沒有
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)賦值運算符是否與類一起自動實現?
Yes
是
No
沒有
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; }Constructor called
構造函數稱為
Program successfully executed – no output
程序成功執行-無輸出
Compile time error
編譯時間錯誤
Run time error
運行時錯誤
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 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
構造函數稱為
Constructor called Constructor called
構造函數稱為構造函數稱為
Compile time error
編譯時間錯誤
Run time error
運行時錯誤
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; }Compile time error
編譯時間錯誤
Run time error
運行時錯誤
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.
像結構一樣,我們可以像下面的示例一樣初始化類的公共數據成員(使用初始化器列表) 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组)的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: kotlin获取属性_Kotlin程序|
- 下一篇: Java String indexOf(