D

Dharmendra Sir • 3.53K Points
Extraordinary

Q. What is the output of the following C++ code?

Code:
#include<iostream> 
using namespace std; 
  
class MyClass
{ 
  public: 
    MyClass(); 
}; 
  
MyClass::MyClass()  { 
    cout << "Constructor is called\n"; 
} 
  
int main() 
{ 
    cout << " Before \n"; 
    MyClass myclass(); 
    cout << " After \n"; 
    return 0; 
}
  • (A) Before Constructor is called After
  • (B) Before After
  • (C) Constructor is called Before After
  • (D) None of the above.
  • Correct Answer - Option(B)
  • Views: 103
  • Filed under category C++

Explanation by: Dharmendra Sir
Note that the line “MyClass maclass();” is not a constructor call. The compiler considers this line as a function declaration that receives no parameters and returns an object of type MyClass.

You must be Logged in to update hint/solution

Discusssion

Login to discuss.

Be the first to start discuss.


Question analytics