- #1
member 428835
Can't figure out the code below why the second output in main outputs 101 instead of 1. Something about calling ob1.reverse(s) before the pointer points to reverse(s), but I'm confused. Any help is much appreciated.
C++:
#include <iostream>
class Solution {
private:
int r = 0;
int new_num = 0;
public:
int reverse(int s_) {
while(s_ > 0) {
r = s_ % 10;
new_num = new_num*10 + r;
s_ = (s_ - r)/10;
}
return new_num;
}
};
int main()
{
int s = 10;
Solution ob1;
auto sol = ob1.reverse(s);
std::cout << sol <<"\n";
Solution* ptr = &ob1;
int a = ptr->reverse(s);
std::cout << a <<"\n";
}