Your question is Output Prediction with Pointers. Take a moment with it on the right.
Talk me through your thinking if you like. When you're confident, submit your answer and I'll grade it like a real screen (7/10 or better passes).
Mavenir's packet-processing layer uses a small counter class to track sequence numbers on a network packet buffer. Here is a self-contained program built from that class:
#include <iostream>
using namespace std;
class PacketCounter {
private:
int seq;
friend void bump(PacketCounter* pc, int by);
public:
PacketCounter(int s) : seq(s) {}
int getSeq() const { return seq; }
};
void bump(PacketCounter* pc, int by) {
pc->seq += by;
}
int trackPackets(PacketCounter* pc, int n) {
static int callCount = 0;
callCount++;
for (int i = 0; i < n; i++) {
bump(pc, callCount);
}
return pc->seq;
}
int main() {
PacketCounter counter(10);
PacketCounter* ptr = &counter;
cout << trackPackets(ptr, 2) << endl;
cout << trackPackets(ptr, 2) << endl;
bump(ptr, -5);
cout << counter.getSeq() << endl;
return 0;
}
Trace through this program and give the exact three values it prints, in order. Explain why the second call to trackPackets behaves differently from the first, and what capability the friend declaration on bump gives it that a normal free function would not have.