Q:

C++ operator overloading and parameterized constructors

Why does the below C++ program output "ABaBbAc"? 
#include "stdafx.h"
#include <iostream>

using namespace std;

class A {
public:
    int i;
    A(int j=0):i(j)
    {
        cout<<"A";
    }
    operator int()
    {
        cout<<"a";
        return 2;
    }
};

class B {
public:
    B(int j=1):i(j){
        cout<<"B";
    }
    operator int() {
        cout<<"b";
        return 3;
    }
    int i;
};

int operator+(const A&a, const B&b){
    cout<<"C";
    return a.i + b.i;
}

int main()
{
    A a;
    B b;

    int i = (A)b + (B)a;
    return 0;
}

A:

First, a and b are default constructed (in this order), and this causes AB to be printed to the standard output.
After this, the output of the program may be different from the one you are observing (or may be exactly the one you are observing - read on). 
This is because the operands of operator + do not need to be evaluated in a definite order. They are only granted to be evaluated before operator + is invoked (see paragraph 1.9/15 of the C++11 Standard). 
So this expression:
(A)b

Will result in a construction of a temporary object of type A given an object of type B. How is this possible? Well, A has a constructor accepting an int, and B has a user-defined conversion to int. 
So the user-defined conversion from b to int (1) is responsible for printing a b. Then, the construction of the A temporary from the resulting int (2) is responsible for printing an A.
Symmetrically, the expression:
(B)a

Will result in the construction of a temporary object of type B, constructed from the result of the user-defined conversion of a into int. This conversion (3) is  responsible from printing a, while the construction of the B temporary (4) is responsible for printing B.
Eventually, the two temporaries resulting from the evaluation of the expressions (A)b and (B)a are used as argument for the operator + (5), which is responsible for printing a C.
Now the C++ Standard only specifies that:

(1)-(4) must be evaluated before (5)
(1) must be evaluated before (2)
(3) must be evaluated before (4)

Other than that, the ordering of the evaluations is unspecified. Which means the output must be something like:
AB????C

Where the question marks should be replaced by an admissible permutation of the outputs of (1), (2), (3), and (4) that satisfies the constraints specified above.

