【发布时间】:2013-12-10 19:58:03
【问题描述】:
在提示用户输入两个时间值后,我在按升序输出一个类的两个对象(t1 和 t2)时遇到了一些问题。我知道 bool 和 if 构造的结构方式存在一些错误。任何帮助将不胜感激!
bool lessthan(Time t2) //For two Time objects t1 and t2, t1.lessthan(t2) returns true if t1 is less than, or comes before t2.
{
if (hours < t2.hours)
{
return true;
}
if (minutes < t2.minutes)
{
return true;
}
if (seconds < t2.seconds)
{
return true;
}
return false;
}
bool greaterthan(Time t2) //For two Time objects t1 and t2, t1.greaterthan(t2) returns true if t1 is greater than, or comes after t2.
{
if (hours > t2.hours)
{
return true;
}
if (minutes > t2.minutes)
{
return true;
}
if (seconds > t2.seconds)
{
return true;
}
return false;
}
bool equalto(Time t2) //For two Time objects t1 and t2, t1.equalto(t2) returns true if t1 is equal to, or is the same time as t2.
{
if (hours == t2.hours)
{
return true;
}
if (minutes == t2.minutes)
{
return true;
}
if (seconds == t2.seconds)
{
return true;
}
return false;
}
在主要功能中,我有以下代码:
cout << "\nTime values entered in ascending order: "<<endl;
if (t1.lessthan(t2))
t1.write();
cout << endl;
t2.write();
cout << endl;
【问题讨论】: