2 ms·
This is only a problem is the methods on Stack are virtual. If they are non-virtual, inheritance works like your composition example: inheriting from Base basic
by edflsafoiewq 6y ago
This is only a problem is the methods on Stack are virtual. If they are non-virtual, inheritance works like your composition example: inheriting from Base basically creates a Base member variable, except missing methods can be automatically forwarded to the Base member. This saves you from having to explicitly wrap all the methods of Stack in CountingStack.
Virtual should be used sparingly.
- taejo 6y agoAgreed. Of the OOP languages I use, only C++ by default, and I think this is one of the few defaults in C++ that is correct. In Java terms, every method should be `final` except those that are explicitly intended as customization points.