This does not work, because multi(5, 5); creates a temporary object of class multi , which is immediately destroyed, because it is not used for anything
Since multiply() is a member function of the multi class, it has access to private members, so it can just set a and b directly. You can get the expected result by rewriting multiply as follows:
void multiply() { cout << "\n\n" << a*b; b = 5; cout << "\n" << a*b; }
source share