ostream &operator<<(ostream &os, const Clock &c){
// format the output - if single digit, then needs to be padded with a 0
int hours = c.getHour();
// if hour is 1 digit, then pad with a 0, otherwise just print the hour
(hours < 10) ? os << '0' << hours : os << hours;
return os; // return the stream
}
我使用了一个三元运算符,但是它可以转换成如下的 if/else 语句
if(c.hour < 10){
os << '0' << hours;
}
else{
os << hours;
}