C++ | Constructors | Question 15

filter_none

edit
close

play_arrow

link
brightness_4
code

#include<iostream>
using namespace std;
   
class Test
{
public:
  Test();
};
   
Test::Test()  {
    cout << " Constructor Called. ";
}
   
void fun() {
  static Test t1;
}
   
int main() {
    cout << " Before fun() called. ";
    fun();
    fun();
    cout << " After fun() called. ";  
    return 0;
}

chevron_right


(A) Constructor Called. Before fun() called. After fun() called.
(B) Before fun() called. Constructor Called. Constructor Called. After fun() called.
(C) Before fun() called. Constructor Called. After fun() called.
(D) Constructor Called. Constructor Called. After fun() called.Before fun() called.


Answer: (C)

Explanation: Note that t is static in fun(), so constructor is called only once.

Quiz of this Question



My Personal Notes arrow_drop_up


Article Tags :
Practice Tags :


Be the First to upvote.


Please write to us at contribute@geeksforgeeks.org to report any issue with the above content.