1
#include <bits/stdc++.h>
using namespace std;

class A
{

public:
A()
   {
    cout<<"A is called";

    }
}a;

int main()

{
    cout<<"main is called";

}

but here the A funtion is called first and main is called later what is the mechanism behind it ?

  • 3
    read about static storage duration, `a` is global variable, so it is created before entering `main` function. – rafix07 Sep 16 '18 at 20:55
  • 3
    `#include ` should not be used ([why](https://stackoverflow.com/questions/31816095/why-should-i-not-include-bits-stdc-h)) and `using namespace std;`should be avoided ([why](https://stackoverflow.com/questions/1452721/why-is-using-namespace-std-considered-bad-practice)). Together they reinforce some of the other's worst behaviours, resulting in some very hard to understand errors. Do not do this. – user4581301 Sep 16 '18 at 21:05

1 Answers1

0

What is going on is that the global variable a of type A is created first. In the construtor you output the text A is called and than the program starts with the main funtion.

So what you see is to be expected and you take from it that prior to the execution of main() global variables have to be created.

You can find more information if you look for static storage duration, which global objects have.

NOTE: The order of creation of objects with static storage duration is unspecified by the standard, so any order can be observed.