I have a homework assignment, and a small part of that assignment asks for a abstract parent “Movie” class with three concrete subclasses: “Comedy,” “Drama,” and “Classical.”
Each subclass will never have to compare against a class other than their own. E.g. Comedies will only compare with other comedies, dramas with dramas, etc.
Is there a way to make a single pure virtual comparison method in the abstract parent class “Movie” that takes in one parameter: which would force each concrete subclass to override it, to have a single comparison function to their own class type?
That way the “Comedy” class will be somewhat “forced” to have a comparison function that will compare with another “Comedy” class (Not just any Movie class). Same would apply to Dramas and Classics.
Of course I tried implementing a pure virtual method in Movie that takes in a Movie as a parameter to compare against, that the subclasses would override. Which works, but I want every subclass to have a comparison that would take its own class as a parameter; and the pure virtual method in the base class to somewhat “force” that upon its derivative children.
What you are asking for is not directly possible the way you want. Although you can certainly create an abstract base class method that descendants must override, you cannot force the parameter type at the base class level. Each derived class will have to validate the parameter is the correct type. But you can use a helper class to simplify the task, eg: