练习14.18:
String类的关系运算符就是比较两个字符串字典序的先后。
class String
{friend bool operator<(const String &s1,const String &s2);friend bool operator<=(const String &s1,const String &s2);friend bool operator>(const String &s1,const String &s2);friend bool operator>=(const String &s1,const String &s2);
};bool operator<(const String &s1,const String &s2)
{return strcmp(s1.str,s2.str)<0;
}bool operator<=(const String &s1,const String &s2)
{return strcmp(s1.str,s2.str)<=0;
}bool operator>(const String &s1,const String &s2)
{return strcmp(s1.str,s2.str)>0;
}bool operator>=(const String &s1,const String &s2)
{return strcmp(s1.str,s2.str)>=0;
}
另外2个类有时间再写!!!
练习14.19:
class Date
{friend bool operator<(const Date &d1,const Date &d2);friend bool operator<=(const Date &d1,const Date &d2);friend bool operator>(const Date &d1,const Date &d2);friend bool operator>=(const Date &d1,const Date &d2);
};bool operator<(const Date &d1,const Date &d2)
{return (d1.year < d2.year) || (d1.year==d2.year && d1.month < d2.month) || (d1.year==d2.year && d1.month==d2.month && d1.day < d2.day);
} bool operator<=(const Date &d1,const Date &d2)
{return (d1 < d2) || (d1==d2);
}bool operator>(const Date &d1,const Date &d2)
{return !(d1 <= d2);
}bool operator>=(const Date &d1,const Date &d2)
{return (d1 > d2) || (d1==d2);
}