Open In App
Related Articles

C++ | Constructors | Question 16

Improve Article
Improve
Save Article
Save
Like Article
Like

Predict the output of following program?




#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;
}


(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

Whether you're preparing for your first job interview or aiming to upskill in this ever-evolving tech landscape, GeeksforGeeks Courses are your key to success. We provide top-quality content at affordable prices, all geared towards accelerating your growth in a time-bound manner. Join the millions we've already empowered, and we're here to do the same for you. Don't miss out - check it out now!

Last Updated : 28 Jun, 2021
Like Article
Save Article
Similar Reads