You are passing the shape
by value:
double foo(shape S) { .... }
This means the function has a copy of only the shape
part of the object. This is called object slicing. You can fix this by passing a reference:
double foo(const shape& S) { .... }