OUTPUT OF THE CONSTRUCTOR

FROM THIS SIMPLE CODE
I WAS EXPECTING THE OUTPUT ::

Constructors are automatically called
Sum = 30

Constructors are automatically called
Sum = 60
***********************************************
BUT I GOT

Constructors are automatically called

Constructors are automatically called

Sum = 30
Sum = 60
****************************************************
Which basic concept I am missing ,, Please enlighten me..
****************************************************

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
28
29
30
31
32
33
34
35
36
  #include<iostream>

using namespace std;

class arba {

private:
int a;
int b;
int c;
public:
arba (int a1, int b1, int c1);
int sum ();
};

arba::arba (int a1, int b1, int c1)
{
this->a=a1;
this->b=b1;
this->c=c1;
cout << "\n\nConstructors are automatically called";
}
int arba::sum() {return (a+b+c);};


int main()
{
arba ar(5,10,15);
arba arb(10,20,30);

std::cout << "\nSum = "  << ar.sum();
std::cout << "\nSum = "  << arb.sum();

cout << "\n\n\n";
return 0;
}
Hi,

The output is exactly as it laid out in the code. Code executes in the order that you call it. It starts with main(), then follows with whatever code or functions lie within.

First you created 2 arba objects on lines 28 & 29, so their ctors printed their strings. Next you call the sum function on lines 31 & 32 so they print the answers.

Hope that helps :+)
Topic archived. No new replies allowed.