这个类在程序有错误,大神帮忙指点一下。
头文件程序:#ifndef DTIME_H
#define DTIME_H
#include<iostream>
using namespace std;
class DigitalTime
{
public:
DigitalTime(int theHour,int theMinute);
DigitalTime();
int getHour() const;
int getMinute() const;
void advance(int minutesAdded);
void advance(int hoursAdded,int minutesAdded);
friend bool operator==(const DigitalTime& time1,
const DigitalTime& time2);
friend istream operator>>(istream& ins,DigitalTime& theObject);
friend ostream operator<<(ostream& outs,const DigitalTime& theObject);
private:
int hour;
int minute;
static void readHour(int& theHour);
static void readMinute(int& theMinute);
static int digitToInt(char c);
};
#endif // DTIME_H
函数程序:
#include "dtime.h"
#include<iostream>
#include<cctype>
#include<cstdlib>
using namespace std;
DigitalTime::DigitalTime(int theHour, int theMinute)
{
if(theHour<0||theHour>24||theMinute<0||theMinute>59)
{
cout<<"Illegal argument to DigitalTime constructor.";
exit(1);
}
else
{
hour=theHour;
minute=theMinute;
}
if(hour==24)
{
hour=0;
minute=0;
}
}
DigitalTime::DigitalTime()
{
hour=0;
minute=0;
}
int DigitalTime::getHour()const
{
return hour;
}
int DigitalTime::getMinute() const
{
return minute;
}
void DigitalTime::advance(int minutesAdded)
{
int grossMinutes=minute+minutesAdded;
minute=grossMinutes%60;
int hourAdjustment=grossMinutes/60;
hour=(hour+hourAdjustment)%24;
}
void DigitalTime::advance(int hoursAdded, int minutesAdded)
{
hour=(hour+hoursAdded)%24;
advance(minutesAdded);
}
bool operator ==(const DigitalTime& time1,const DigitalTime& time2)
{
return (time1.hour==time2.hour&&time1.minute==time2.minute);
}
istream& operator <<(istream& ins,DigitalTime& theObject)
{
DigitalTime::readHour(theObject.hour);
DigitalTime::readMinute(theObject.minute);
return ins;
}
ostream& operator >>(ostream& outs,DigitalTime& theObject)
{
outs<<theObject.hour<<":";
if(theObject.minute<10)
outs<<'0';
outs<<theObject.minute;
return outs;
}
int DigitalTime::digitToInt(char c)
{
return (static_cast<int>(c)-static_cast<int>('0'));
}
void DigitalTime::readMinute(int &theMinute)
{
char c1,c2;
cin>>c1>>c2;
if(!(isdigit(c1)&&isdigit(c2)))
{
cout<<"Error:illegal input to readMinute\n";
exit(1);
}
theMinute=digitToInt(c1)*10+digitToInt(c2);
if(theMinute<0||theMinute>59)
{
cout<<"Error:illegal input to readMinute\n";
exit(1);
}
}
void DigitalTime::readHour(int &theHour)
{
char c1,c2;
cin>>c1>>c2;
if(!(isdigit(c1)&&(isdigit(c2)||c2==':')))
{
cout<<"Error:illegal input to readHour\n";
exit(1);
}
if(isdigit(c1)&&c2==':')
{
theHour=DigitalTime::digitToInt(c1);
}
else
{
theHour=DigitalTime::digitToInt(c1)*10
+DigitalTime::digitToInt(c2);
cin>>c2;
if(c2!=':')
{
cout<<"Error: illegal input to readHour\n";
exit(1);
}
}
if(theHour==24)
theHour=0;
if(theHour<0||theHour>23)
{
cout<<"Error: illegal input to readHour\n";
exit(1);
}
}
主函数程序:
#include<iostream>
using namespace std;
#include"dtime.h"
int main()
{
DigitalTime clock,oldClock;
cout<<"You may write midnight as either 0:00 or 24:00,\n"
<<"but I will always write it as 0:00.\n"
<<"Enter the time in 24-hour notation:";
cin>>clock;
oldClock=clock;
clock.advance(15);
if(clock==oldClock)
cout<<"Something is wrong.";
cout<<"You entered "<<oldClock<<endl;
cout<<"15 minutes later the time will be"<<clock<<endl;
clock.advance(2,15);
cout<<"2 hours and 15 minutes after that\n"
<<"the time will be "
<<clock<<endl;
return 0;
}
程序中istream和ostream函数部分有错误应该怎么改