73.9k views
0 votes
What is the output of the following program? #include using namespace std; class TestClass { public: TestClass(int x) { cout << x << endl; } TestClass() { cout << "Hello!" << endl; } }; int main() { TestClass test; return 0; }

User Caiuspb
by
5.5k points

1 Answer

5 votes

Answer:

The answer is "Hello!".

Step-by-step explanation:

  • In the given C++ language program, a class "TestClass " is defined, which contains a parameterized constructor and default constructor.
  • In the parameterized constructor an integer variable "x" is passed in the constructor parameter and inside a constructor parameter value is print. Inside the default constructor, a print function (cout) is used that print message "Hello!".
  • In the main method, a class "TestClass" object is created that is "test" when a class object is created it automatically called a default constructor that's why the output to this code is "Hello!".

User Mysteryos
by
5.6k points