Skip to main content

Posts

Showing posts with the label Templates versus Inheritance. mathematical applications

Templates versus Inheritance

Programmers sometimes find it tricky to decide whether to use templates or inheritance. Here are some tips to help you make the decision. Use templates when you want to provide identical functionality for different types. For example, if you want to write a generic sorting algorithm that works on any type, use templates. If you want to create a container that can store any type, use templates. The key concept is that the templatized structure or algorithm treats all types the same. When you want to provide different behaviors for related types, use inheritance. For example, use inheritance if you want to provide two different, but similar, containers such as a queue and a priority queue. Inheritance gets quite useful when designing user interface components -- e.g. when your Application Window inherits from general Window (this type of relationship is called is-a relationship and it's one of the major uses of inheritance). Similarly for mathematical applications -- say, yo...