Skip to content
Related Articles

Related Articles

Improve Article

C++ | Constructors | Question 16

  • Difficulty Level : Basic
  • Last Updated : 28 Jun, 2021
Geek Week

Predict the output of following program?

#include <iostream>
using namespace std;
class Test
    int x;
    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




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


Quiz of this Question

Take a step-up from those “Hello World” programs. Learn to implement data structures like Heap, Stacks, Linked List and many more! Check out our Data Structures in C course to start learning today.
My Personal Notes arrow_drop_up
Recommended Articles
Page :