Skip to content
Related Articles
Get the best out of our app
GeeksforGeeks App
Open App

Related Articles

C++ | Constructors | Question 6

Improve Article
Save Article
Like Article
Improve Article
Save Article
Like Article

Output of following C++ code will be?


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


Compiler Error


10 followed by Garbage Value


10 10


10 0

Answer: (C)


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
Please comment below if you find anything wrong in the above post

My Personal Notes arrow_drop_up
Last Updated : 28 Jun, 2021
Like Article
Save Article
Similar Reads