When we implement add function by operator + in C++, sometimes we face over error - actually system does not show any error and that should be continuously increased, but there is a possibility to change into minus value suddenly.
You can see the same case when you compile and run by below code:
Below safe_add() function enables you to check overflow error:
Time Complexity : O(1)
Space Complexity: O(1)