C Program #2

What is the output of the below C program?

#include <iostream>
using std::cout;
class Test
{
public:
	Test();
	~Test();
};
Test::Test()
{
	cout << "Constructor is executed\n";
}
Test::~Test()
{
	cout << "Destructor is executed\n";
}
int main()
{
	delete new Test();
	return 0;
}

Output

Constructor is executed
Destructor is executed

The first line of the main () function appears strange, but it is entirely legitimate. In C++, an object can be created without having its handle assigned to any pointer. Without any pointers pointing at it, this statement will produce an object of the class Test.

Leave Your Comment