Why constructor is executed before main function ??

Hi,
I wrote two programme to understand behavior. There are two question, each of them are after code output

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
//Programme 1
#include <iostream>
using namespace std;

class Rectangle {
    int width, height;
  public:
    Rectangle ();
    Rectangle (int,int);
    int area (void) {return (width*height);}
};

Rectangle::Rectangle () {
  cout<<"Default COnstructor"<<endl;
}

Rectangle::Rectangle (int a, int b) {
  width = a;
  height = b;
}

int main () {
  Rectangle rect (3,4);
  Rectangle rectb;
  cout << "rect area: " << rect.area() << endl;
  return 0;
}


ouput is :
Default COnstructor <--- Why ?? It should be executed after parameterized consttructor
12

Similarily, programme 2

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
// overloading class constructors
#include <iostream>
using namespace std;

class Rectangle {
    int width, height;
  public:
    Rectangle ();
    Rectangle (int);
};

Rectangle::Rectangle () {
  cout<<"Default COnstructor"<<endl;
}

Rectangle::Rectangle (int a) {
  cout<<"Parameterized constructor"<<endl;
}

int main () {
  Rectangle rect (3);
  Rectangle rectb;
  return 0;
}


output:
Parameterized constructor
Default COnstructor

Why different order ?
In the first program you are not printing anything from the parameterized constructor.
There is no cout in Rectangle(int a, int b).
Topic archived. No new replies allowed.