Hi All,

Why the output of the following program is 0231??
Please resolve my doubt.

#include <iostream>

using namespace std;
    class B
    {
        int data;
    public:
        B() { data =0;}
        int func(){cout<<data++;return data++;        }
    };

int main()
{
    B b;
    cout<<b.func()<<b.func()<<endl;
    return 2;
}

You are incrementing by 1, and, outputting the method. Also, your class is wrong:

#include <iostream>

using namespace std;

class B {

    public:
        B() { };

        int func() {
            return this->data++;
        }
    private:
        int data;
};

int main(int argc, char *argv[]) {

    B b;

    cout << b.func() << endl;
    cout << b.func() << endl;
}

Output: 0 1

If you have an int method data type then the compiler expects you to return an integer but in this case you outputted and then returned the integer which is probably why you were getting the output.

Hope this helps

I didn't want to change/improve the program, just wanted to know how the call chain is going on.
Please tell me.

It mostly has to do with the order of evaluation in your code, which is "implementation defined". IE, on one compiler it may work like you would expect, and another it won't (as this is an example). Consider this, when you call func() the first time, it outputs 0, and returns 1, yet data now has a value of 2 (the value of data before the second increment is placed on the return stack before the increment). The issue is in the line cout<<b.func()<<b.func()<<endl; in main. Since the output is 0231 and not 0123, the compiler is valuating the second call of b.func() before the first (right to left instead of the intuitive left to right), yet the results of the first is output before the return value of the second. Confused yet? :-)

So, the second is evaluated first - printing 0, returning 1. The first is evaluated next, printing 2, returning 3. The return value from the first is then printed in the line in main before the return value of the second, resulting in 0 (from the second), 2 (from the first), 3 (return value from first), and then 1 (return value from second). Confused yet? :-) Welcome to the world of C/C++ compiler quirks!

In any case, this is a good example of a compiler that evaluates arguments to functions in a right-to-left manner (common, due to the use of recursion in building parsers). It is also a good reason to avoid stuff like this: int a = 0; aFunction(a++, a++, a++); In some situations (compilers), the first argument may be 0, the second 1, and the third 2, with the final value of a being 3. However, in a right-to-left evaluation, the first argument would be 2, the second 1 (no change there), and the third 0, yet a will still be 3 when done. Assuming aFunction does different things depending upon the values of its arguments, its results may be totally different from one system to another!

Anyway, that was a great (and informative) question mahesh! Keep asking ones like this, and in 10 or 20 years you will be a C/C++ programming guru! :-)

This article has been dead for over six months. Start a new discussion instead.