设计一个时间类CTimeInfo,要求其满足下述要求。 (1) 具有私有成员:int...
发布网友
发布时间:2024-10-11 16:16
我来回答
共1个回答
热心网友
时间:2024-10-11 16:18
#include <iostream>
#include <string>
class CTimeinfo
{
private:
int hour,minute;
public:
CTimeinfo();
CTimeinfo(int _Hour,int _Minute);
bool set(int _Hour,int _Minute);
friend class CMeeting;
};
class CMeeting
{
private:
std::string topic;
std::string ownername;
CTimeinfo starttime;
CTimeinfo endtime;
public:
CMeeting(std::string _Topic,std::string _OwnerName);
void settime(CTimeinfo _StartTime,CTimeinfo _EndTime);
CTimeinfo lasttime();
void show();
};
CTimeinfo::CTimeinfo():hour(0),minute(0){}
CTimeinfo::CTimeinfo(int _Hour,int _Minute):hour(_Hour),minute(_Minute){}
bool CTimeinfo::set(int _Hour,int _Minute)
{
if( _Hour>=0 && _Hour <24 && _Minute>=0 && _Minute<60 )
{
hour=_Hour;
minute=_Minute;
return true;
}
else
{
hour=0;
minute=0;
return false;
}
}
CMeeting::CMeeting(std::string _Topic,std::string _OwnerName):topic(_Topic),ownername(_OwnerName){}
void CMeeting::settime(CTimeinfo _StartTime,CTimeinfo _EndTime)
{
starttime.hour=_StartTime.hour;
starttime.minute=_StartTime.minute;
endtime.hour=_EndTime.hour;
endtime.minute=_EndTime.minute;
}
CTimeinfo CMeeting::lasttime()
{
CTimeinfo temp;
temp.hour=endtime.hour-starttime.hour;
temp.minute=endtime.minute-starttime.minute;
if(temp.minute<0)
{
temp.hour--;
temp.minute+=60;
}
if(temp.hour<0)
temp.hour+=24;
return temp;
}
void CMeeting::show()
{
std::cout<<"Topic: "<<topic<<std::endl
<<"OwnerName: "<<ownername<<std::endl
<<"StartTime: "<<starttime.hour<<':'<<starttime.minute<<std::endl
<<"EndTime: "<<endtime.hour<<':'<<endtime.minute<<std::endl
<<"LastTime:"<<lasttime().hour<<':'<<lasttime().minute<<std::endl;
}
int main()
{
CMeeting m(std::string("Sein und Zeit"),std::string("Martin Heidegger"));
m.settime(CTimeinfo(5,26),CTimeinfo(9,26));
m.show();
return 0;
}时间输入不合法置0。开始时间晚于结束时间认为经过一夜。