Consider a class calendar that stores a bunch of Date objects. A calendar is designed to store a collection of objects of any type that inherit from Date. I thought the best way to do this is to create a class template like
template<typename D> class Calendar{
...
}
But it seemed to me that D can now be any class. Now my question is: how can I make sure that D is a subclass of the date object?
I know how to do this, this is Java, but I'm still not familiar with the C ++ syntax. The problem is very similar to how some collections can only accept template variables that implement Comparable. Then the headline would look like
public class Calendar<D extends Date>{
...
}
-------------------- EDIT: ------------------------- --- --------------
, . . , Calendar<Gregorian>, Date, , . . , Calendar<Gregorian>, Calendar<Julian>. :
Calendar<Gregorian> cal;
std::cout << "These events are entered as dates in
the Gregorian calendar" << std::endl;
cal.add_event("Christmas", 12, 25);
cal.add_event("Gregorian new year", 1, 1);
std::cout << cal << std::endl;
std::cout << "----" << std::endl;
std::cout << "And printed out as Julian dates" << std::endl;
Calendar<Julian>(cal);
std::cout << cal<< std::endl;
:
These events are entered as dates in the Gregorian calendar
2009-12-25 Christmas
2010-01-01 Gregorian new year
----
And printed out as Julian dates
2009-12-13 Christmas
2009-12-19 Gregorian new year
------------- : ----------------------
. .
.
, , OO , Polymorphism .. , ++, , Java, , .