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.
πŸ’¬ Discuss
βœ… Correct Answer: (B) Before After
Explanation: 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.
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.

πŸ’¬ Discussion


πŸ“Š Question Analytics

πŸ‘οΈ
221
Total Visits
πŸ“½οΈ
4 y ago
Published
πŸŽ–οΈ
Dharmendra Sir
Publisher
πŸ“ˆ
87%
Success Rate