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..
****************************************************
#include<iostream>
usingnamespace 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;
}
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.