C++ | Constructors | Question 6
#include<iostream> using namespace std; class X { public: int x; }; int main() { X a = {10}; X b = a; cout << a.x << " " << b.x; return 0; } |
chevron_right
filter_none
(A) Compiler Error
(B) 10 followed by Garbage Value
(C) 10 10
(D) 10 0
Answer: (C)
Explanation: The following may look like an error, but it works fine.
X a = {10};
Like structures, class objects can be initialized.
The line “X b = a;” calls copy constructor and is same as “X b(a);”. Please note that, if we don’t write our own copy constructor, then compiler creates a default copy constructor which assigns data members one object to other object.
Quiz of this Question
Recommended Posts:
- C++ | Constructors | Question 15
- C++ | Constructors | Question 5
- C++ | Constructors | Question 4
- C++ | Constructors | Question 7
- C++ | Constructors | Question 8
- C++ | Constructors | Question 9
- C++ | Constructors | Question 13
- C++ | Constructors | Question 12
- C++ | Constructors | Question 17
- C++ | Constructors | Question 10
- C++ | Constructors | Question 3
- C++ | Constructors | Question 14
- C++ | Constructors | Question 16
- C++ | Constructors | Question 17
- C++ | Constructors | Question 1



