c++实现clock类
c++初学者真的会被老师的作业弄疯掉!!!
本章以完成作业之便~顺手发一下,或许可以帮助小伙伴解惑。
作业:根据给出的函数名称表述正确的函数体和在main函数上体现该函数的功能
PART 1:头文件和命名空间
#include
#include
using namespace std;
PART 2:clock类
class Clock {
public:Clock() ; //构造函数==没有参数的函数Clock(int h, int m, int s);//有参数的构造函数void setclock(int newH,int newM,int newS); //设置新的时间int gethour() { return hour; }; //获取小时int getmin() { return min; }; //获取分钟int getsecd() { return sec; }; //获取秒int secondbetween(int a,int b,int c); //增加时间的函数Clock clockadd(int h, int m, int s); //时分秒相加Clock clocksub(int h, int m, int s); //时分秒相减Clock operator+=(int second); //秒相加Clock operator-=(int second); //秒相减//重载运算符Clock operator++(); //前缀++Clock operator++(int); //后缀++Clock operator--(); //前缀--Clock operator--(int); //后缀--Clock operator+(int seconds); //秒相加Clock operator-(int seconds); //秒相减void showtime(); //显示时间//重载运算符双目friend long operator-(const Clock& T1, const Clock& T2); //T1-T2//时间比较大小friend bool operator <(const Clock& T1, const Clock& T2); //T1(const Clock& T1, const Clock& T2); //T1>T2friend bool operator ==(const Clock& T1, const Clock& T2);//T1==T2friend bool operator >=(const Clock& T1, const Clock& T2);//T1>=T2friend bool operator <=(const Clock& T1, const Clock& T2);//T1<=T2friend bool operator !=(const Clock& T1, const Clock& T2);//T1!=T2
private:int hour, min, sec;
};
PART 3:类内函数体的实现
clock(); //无参数的构造函数(默认构造函数)
Clock::Clock() :hour(0), min(0), sec(0) //没有参数的函数
{ }
Clock(int h,int m,int s); //带参数的构造函数
Clock::Clock(int h,int m,int s):hour(h),min(m),sec(s)
{ }
void setclock(int newH,int newM,int newS); //设置时间的函数
void Clock::setclock(int newH, int newM, int newS) {hour = newH;min = newM;sec = newS;
}
返回值类型 类名::函数成员名(参数表)
int secondbetween(int a,int b,int c); //增加时间的函数
int Clock::secondbetween(int a, int b,int c) { //增加时间的函数hour = a;min = b;sec = c;return a * 60 + b * 60 + c;
}
Clock clockadd(int h,int m,int s); //时间相加
Clock Clock::clockadd(int h, int m, int s) {sec = sec + s;if (sec >= 60){sec = sec - 60;min++;}min = min + m;if (min >= 60){min = min - 60;hour++;}hour = hour + h;if (hour > 24)hour = hour - 24;return *this;
}
Clock clocksub(int h,int m,int s);
Clock Clock::clocksub(int h, int m, int s) {sec = sec - s; if (sec < 0){sec = sec + 60;min--;}min = min - m;if (min < 0) {min = min+60 ;hour --;}hour = hour - h;if (hour < 0){hour += 24;}return *this;
}
Clock operator+=(int second); //秒相加
Clock operator-=(int second); //秒相减
Clock Clock::operator+=(int second) {sec += second;return *this;
}
Clock Clock::operator-=(int second) {sec -= second;return *this;
}
重载运算符
Clock operator++(); //++c;
Clock operator++(int); //c++;
Clock Clock::operator++() {sec++;if (sec >= 60){min += sec / 60;sec %= 60;if (min > 60){hour += min / 60;min %= 60;if (hour >= 24)hour %= 24;}}return * this;
}Clock Clock::operator++(int) {Clock old = (*this);++(*this);return (old);
}
Clock operator--(); //--c
Clock operator--(int); //c--
Clock Clock ::operator--() {sec--;if (sec <= -1){sec = 60 + sec;min--;if (min <= -1){min = 60 + min;hour--;if (hour <= -1){hour = 24 + hour;}}}return *this;
}Clock Clock::operator--(int) {Clock older = *this;sec--;if (sec <= -1){sec = 60 + sec;min--;if (min <= -1){min = 60 + min;hour--;if (hour <= -1){hour = 24 + hour;}}}return older;
}
Clock operator+(int seconds); //秒相加
Clock operator-(int seconds); //秒相减
Clock Clock::operator +(int seconds) {Clock temp = *this;for (int i = 0; i < seconds; i++){temp.sec += 1;if (temp.sec == 60){temp.min += 1;if (temp.min == 60){temp.hour += 1;temp.min = 0;}temp.sec = 0;}} return temp;
}Clock Clock::operator -(int seconds) {Clock temp = *this;for (int i = 0; i < seconds; i++){temp.sec -= 1;if (temp.sec < 0){temp.min -= 1;if (temp.min < 0){temp.hour -= 1;temp.min = 59;}temp.sec = 59;}}return temp;}
void showtime(); //显示时间
inline void Clock::showtime() {cout << hour << ":" << min << ":" << sec << endl;}
重载运算符 双目
friend long operator-(const Clock& T1,const Clock& T2); //T1-T2
long operator-(const Clock& T1, const Clock& T2) {return long(T1.hour * 60 + T1.min * 60 + T1.sec - T2.hour * 60 - T2.min * 60 - T2.sec);
}
时间比较大小
friend bool operator <(const Clock& T1,const Clock& T2); //T1
friend bool operator >(const Clock& T1,const Clock& T2); //T1>T2?
bool operator <(const Clock& T1, const Clock& T2) {return T1.hour * 60*60 + T1.min * 60 + T1.sec < T2.hour * 60*60 + T2.min*60 + T2.sec;}bool operator >(const Clock& T1, const Clock& T2) {return T1.hour * 60*60 + T1.min * 60 + T1.sec > T2.hour * 60*60 + T2.min *60+ T2.sec;}
friend bool operator ==(const Clock& T1,const Clock& T2); //T1==T2?
friend bool operator !=(const Clock& T1,const Clock& T2); //T1!=T2?
bool operator ==(const Clock& T1, const Clock& T2) {
return T1.hour*60*60+T1.min*60*60+T1.sec == T2.hour*60*60+T2.min*60+T2.sec;}bool operator !=(const Clock& T1, const Clock& T2) {
return T1.hour * 60 * 60 + T1.min * 60 + T1.sec != T2.hour * 60 * 60 + T2.min * 60 +T2.sec;}
friend bool operator >=(const Clock& T1,const Clock& T2); //T1>=T2?
friend bool operator <=(const Clock& T1,const Clock& T2); //T1<=T2?
bool operator >=(const Clock& T1, const Clock& T2)
{
return T1.hour * 60*60 + T1.min * 60*60 + T1.sec >= T2.hour * 60*60 + T2.min*60 + T2.sec;}bool operator <=(const Clock& T1, const Clock& T2)
{
return T1.hour * 60 * 60 + T1.min * 60 + T1.sec >= T2.hour * 60 * 60 + T2.min * 60+T2.sec;}
PART 3:main函数实现其功能
int main() {Clock myclock,c1,b(0,0,0),c3;Clock c(0, 0, 0),c2(0,0,0);cout << "构造函数->myclock.showtime()" << endl;myclock.showtime();cout << endl;Clock youclock(10, 30, 30);cout << "有参数的构造函数->youclock.showtime(10,30,30)" << youclock<myclock.setclock(8, 30, 30)" <youclock" <<(myclock > youclock) << endl;cout << endl;cout << "myclock==youclock" <<(myclock == youclock) << endl;cout << endl;cout << "myclock>=youclock" << (myclock >= youclock) <
若有雷同,纯属巧合,可私。
新手写的烂,欢迎批评指正,不喜勿踩。
本文来自互联网用户投稿,文章观点仅代表作者本人,不代表本站立场,不承担相关法律责任。如若转载,请注明出处。 如若内容造成侵权/违法违规/事实不符,请点击【内容举报】进行投诉反馈!