C++ | new and delete | Question 3

Predict the output?

filter_none

edit
close

play_arrow

link
brightness_4
code

#include <iostream>
using namespace std;
  
class Test 
{
  int x;
  Test() { x = 5;}
};
  
int main()
{
   Test *t = new Test;
   cout << t->x;
}

chevron_right


(A) Compiler Error
(B) 5
(C) Garbage Value
(D) 0


Answer: (A)

Explanation: There is compiler error: Test::Test() is private.

new makes call to the constructor. In class Test, constructor is private (note that default access is private in C++).


Quiz of this Question

Don’t stop now and take your learning to the next level. Learn all the important concepts of Data Structures and Algorithms with the help of the most trusted course: DSA Self Paced. Become industry ready at a student-friendly price.

My Personal Notes arrow_drop_up
Article Tags :
Practice Tags :


1


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