C++ | Constructors | Question 16

Predict the output of following program?

filter_none

edit
close

play_arrow

link
brightness_4
code

#include <iostream>
using namespace std;
class Test
{
private:
    int x;
public:
    Test(int i)
    {
        x = i;
        cout << "Called" << endl;
    }
};
  
int main()
{
    Test t(20);
    t = 30; // conversion constructor is called here.
    return 0;
}

chevron_right


(A) Compiler Error
(B)

Called
Called

(C)



Called


Answer: (B)

Explanation: If a class has a constructor which can be called with a single argument, then this constructor becomes conversion constructor because such a constructor allows automatic conversion to the class being constructed.

A conversion constructor can be called anywhere when the type of single argument is assigned to the object. The output of the given program is

Called
Called


Quiz of this Question



My Personal Notes arrow_drop_up


Article Tags :
Practice Tags :


Be the First to upvote.


Please write to us at contribute@geeksforgeeks.org to report any issue with the above content.