Whenever you're ready, walk me through how you'd approach this.
Citadel Securities' trading systems team keeps a small order class hierarchy in its onboarding material, because new engineers are expected to reason precisely about how C++'s reference, pointer, and pass-by-value semantics interact with virtual dispatch and object slicing before touching the real order book code.
#include <iostream>
using namespace std;
class Order {
public:
virtual double price() const { return 100.0; }
virtual ~Order() {}
};
class LimitOrder : public Order {
public:
double price() const override { return 150.0; }
};
class MarketOrder : public Order {
public:
double price() const override { return 200.0; }
};
void printPrice(const Order& o) {
cout << o.price() << endl;
}
void printPriceByValue(Order o) {
cout << o.price() << endl;
}
int main() {
LimitOrder lo;
MarketOrder mo;
Order oSliced = lo;
printPrice(lo);
printPrice(oSliced);
printPriceByValue(lo);
Order* ptr = &mo;
cout << ptr->price() << endl;
return 0;
}
Trace through main() line by line and state exactly what this program prints, in order. Explain the reasoning behind each printed value.