Open In App
Related Articles

C++ | Constructors | Question 9

Improve
Improve
Improve
Like Article
Like
Save Article
Save
Report issue
Report
Predict the output of following program.
#include<iostream>
#include<stdlib.h>
using namespace std;
  
class Test
{
public:
   Test()
   { cout << "Constructor called"; }
};
  
int main()
{
    Test *t = (Test *) malloc(sizeof(Test));
    return 0;
}

                    
(A) Constructor called (B) Empty (C) Compiler Error (D) Runtime error

Answer: (B)

Explanation: Unlike new, malloc() doesn’t call constructor (See this) If we replace malloc() with new, the constructor is called, see this.

Quiz of this Question

Last Updated : 28 Jun, 2021
Like Article
Save Article
Previous
Next
Share your thoughts in the comments
Similar Reads