Prefer composition over inheritance?
Why prefer composition over inheritance? What trade-offs are there for each approach? When should you choose inheritance over composition?
Why prefer composition over inheritance? What trade-offs are there for each approach? When should you choose inheritance over composition?
The error is because of the declaration: The vector contains pointers to mutable ships, but the code passes a pointer to a const ship to push_back. You either need to store const pointers in the vector: Or pass a non const pointer to the push_back: Side note: move the above function to a cpp, move it … Read more
They are absolutely different. Inheritance is an “is-a” relationship. Composition is a “has-a”. You do composition by having an instance of another class C as a field of your class, instead of extending C. A good example where composition would’ve been a lot better than inheritance is java.util.Stack, which currently extends java.util.Vector. This is now … Read more