Skip to content
Related Articles
Get the best out of our app
GFG App
Open App
geeksforgeeks
Browser
Continue

Related Articles

C++ | new and delete | Question 3

Improve Article
Save Article
Like Article
Improve Article
Save Article
Like Article

Predict the output?




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


(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

My Personal Notes arrow_drop_up
Last Updated : 28 Jun, 2021
Like Article
Save Article
Similar Reads