// file: date.h
#include <cstdio>
class date
{
friend bool operator<(date d1, date d2);
public:
// constructor
date(int y=2015, int m=1, int d=1) : year(y), month(m), day(d) { }
// explicit constructor
explicit date(const char *s);
private:
int year;
int month;
int day;
};
// question: use member or global operators?
bool operator<(date d1, date d2);
inline bool operator==(date d1, date d2) { return !(d1<d2 || d2<d1); }
inline bool operator!=(date d1, date d2) { return d1<d2 || d2<d1; }
inline bool operator<=(date d1, date d2) { return !(d2<d1); }
inline bool operator>=(date d1, date d2) { return !(d1<d2); }
inline bool operator>(date d1, date d2) { return d2<d1; }
// file: date.cpp
date:: date(const char *s)
{
sscanf(s, "%d.%d.%d", &year, &month, &day);
}
bool operator<(date d1, date d2)
{
return d1.year < d2.year || d1.month < d2.month || d1.day < d2.day;
}
// file: main.cpp
int main()
{
date *p = new date;
date *q = new date(*p);
date *s = new date[10];
delete p;
delete p; // undefined behaviour
delete s; // undefined behaviour
}
Ly8gZmlsZTogZGF0ZS5oCiNpbmNsdWRlIDxjc3RkaW8+CgpjbGFzcyBkYXRlCnsKICBmcmllbmQgYm9vbCBvcGVyYXRvcjwoZGF0ZSBkMSwgZGF0ZSBkMik7CnB1YmxpYzoKICAvLyBjb25zdHJ1Y3RvciAgICAKICBkYXRlKGludCB5PTIwMTUsIGludCBtPTEsIGludCBkPTEpIDogeWVhcih5KSwgbW9udGgobSksIGRheShkKSB7IH0KICAvLyBleHBsaWNpdCBjb25zdHJ1Y3RvcgogIGV4cGxpY2l0IGRhdGUoY29uc3QgY2hhciAqcyk7CnByaXZhdGU6CiAgaW50IHllYXI7CiAgaW50IG1vbnRoOwogIGludCBkYXk7Cn07CgovLyBxdWVzdGlvbjogdXNlIG1lbWJlciBvciBnbG9iYWwgb3BlcmF0b3JzPwpib29sIG9wZXJhdG9yPChkYXRlIGQxLCBkYXRlIGQyKTsKaW5saW5lIGJvb2wgb3BlcmF0b3I9PShkYXRlIGQxLCBkYXRlIGQyKSB7IHJldHVybiAhKGQxPGQyIHx8IGQyPGQx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