Simple question here. I have to define classes that will contain sets of books, movies, CDs. I defined a template class, say Set<T>, with all the common methods, and then defined classes that derive from a particular instance (is it the correct term?) of the Set class
class Movies_set : public Set<Movie>
{
/* ... */
}
in which I define some other methods (say a function to search for actors). Is it a good programming technique or are there more convenient methods?
It depends on what Set actually is. I suspect Set is something similar to std::set and in that case, I would advice against derivation and steer you towards composition.
The reason deriving from Set is a bad idea is because it exposes the API of set, which has nothing to do whatsoever with MovieSet. If you need methods to make searches to your MovieSet object, implement them on MovieSet itself, especially since, as you pointed out, you are going to augment MovieSet’s interface.
This way, you would also gain something very powerful : encapsulation. If for some reason you decide to change from a Set to a std::map, the interface of your base class is likely to change and any code that accesses MovieSet will be impacted. If you had stored Set as a private data member, you would not run into this issue, as you would have already defined MovieSet’s appropriate accessors.
That of course, if all based on my premise that Set is actually just a container.