Skip to content
Related Articles

Related Articles

C++ | Constructors | Question 6

View Discussion
Improve Article
Save Article
  • Difficulty Level : Medium
  • Last Updated : 28 Jun, 2021

using namespace std;
    int x;
int main()
    X a = {10};
    X b = a;
    cout << a.x << " " << b.x;
    return 0;

(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

My Personal Notes arrow_drop_up
Recommended Articles
Page :

Start Your Coding Journey Now!