In the main file when I comment the c pointer and its related lines the output is correct i.e 20 and 42. However when it is uncommented output for both b and c is correct but the output for a is a random number.I am unable to track the sequence of operations.Could U please help.
Okay, first off, I hope that is just for exercise, because under ordinary circumstances, allocating x and y dynamically is a horrible, horrible idea.
Aside from that, here: Point *c;
c->set_points(7,9);
you create an uninitialized pointer c that doesn't point to anything valid.
Thus, dereferencing c in the next line results in undefined behavior.
You either can do this: Point c;
c.set_points(7,9);
or if you want to allocate the Point object dynamically too (again, there is no reason to do so in this case either):
And set_points leaks memory, because it unnecessarily creates another two int instances (and without deleting the old ones), instead of just reassigning the values.