千博企业网站管理系统,设计类专业需要艺考吗,网站网页制作电话,好姑娘高清免费观看介绍 本文主要介绍 减号( - )、加号( )、加等()运算符重载并复现了时钟运行方式 源码
#includeiostreamusing namespace std;class Time
{
public://加号计算函数void t_add(int temp, const int Right, bool b){//判断时间是否大于 60if ((temp Rig…介绍 本文主要介绍 减号( - )、加号( )、加等()运算符重载并复现了时钟运行方式 源码
#includeiostreamusing namespace std;class Time
{
public://加号计算函数void t_add(int temp, const int Right, bool b){//判断时间是否大于 60if ((temp Right) 60)//当两数相加 小于60 则直接相加{temp Right;b 0;}else//当两数相加 大于60 则直接相加 后在减去60秒(分){temp Right - 60;b 1;}}//减号计算函数void t_sub(int Left, const int Right, bool b){//判断左操作数是否大于右操作数if (Left Right){//当左操作数大于右操作数则 直接两数相减Left - Right;b 0;}else{//当左操作数小于右操作数则 直接两数相减 后加上 60 秒(分)Left (Left - Right) 60;b 1;}}//打印结果void print(){cout 时间为 m_Hour : m_minute : m_second endl;}
public://构造函数Time(int hour 0, int minute 0, int second 0) :m_Hour(hour), m_minute(minute), m_second(second){}//加号重载Time operator( const TimeRight){Time temp *this;//用临时变量来接收 左操作数bool b 0;//用来接收是否超过时间进制//秒针时间判断temp.t_add(temp.m_second, Right.m_second, b);if (b 1)//b 1代表秒针超过 60 秒了需要分针加 1 分钟{/**************************************************利用 三目运算符 判断分针加 1 分针后是否超过 60 分针1.如果没超过则 “当前时间中 分针”加 1 分针2.如果超过则 “当前时间中 时针”加 1 小时**************************************************/(temp.m_minute 1) 60 ? (temp.m_minute 1) : (temp.m_Hour 1);}//分针时间判断temp.t_add(temp.m_minute, Right.m_minute, b);if (b 1)//b 1代表分针超过 60 分针了需要时针加 1 小时{/**************************************************利用 三目运算符 判断时针加 1 小时后是否超过 24 小时1.如果没超过则 “当前时间中 时针”加 1 小时2.如果超过则归 0 小时**************************************************/(temp.m_Hour 1) 24 ? (temp.m_Hour 1) : (temp.m_Hour 0);}//时针时间判断/**************************************************利用 三目运算符 判断两个数相加是否超过 24 小时1.如果没超过则 相加两数2.如果超过则 先相加两数,然后减 24 小时**************************************************/(temp.m_Hour Right.m_Hour) 24 ? (temp.m_Hour temp.m_Hour Right.m_Hour) : (temp.m_Hour (temp.m_Hour Right.m_Hour) - 24);b 0;//初始化 bool return temp;//返回临时变量}//减号重载Time operator-(const TimeRight){Time temp *this;//用临时变量来接收 左操作数bool b 0;//用来接收是否超过时间进制//秒针时间判断temp.t_sub(temp.m_second, Right.m_second, b);if (b 1)//b 1代表秒针超过 60 秒了需要分针减 1 分钟{/*******************************************************判断减 1 分钟后是否小于等于 01.如果小于等于0 则先减 1 分针 在加 60 分钟,然后减 1 小时2.如果大于0 则减 1 分针*******************************************************/if ((temp.m_minute - 1) 0){temp.m_minute (temp.m_minute - 1) 60;temp.m_Hour - 1;}else{temp.m_minute - 1;}}//分针时间判断temp.t_sub(temp.m_minute, Right.m_minute, b);if (b 1)//b 1代表分针超过 60 分针了需要时针减 1 小时{/*********************************************************利用 三目运算符 判断时针减 1 小时后小于等于 01.如果没超过则 “当前时间中 时针”减 1 小时,然后加 24 小时2.如果大于0 则减 1 小时*********************************************************/(temp.m_Hour - 1) 0 ? temp.m_Hour (temp.m_Hour - 1) 24 : temp.m_Hour - 1;}//时针时间判断/*********************************************************利用 三目运算符 判断 两数相减 是否小于等于 01.如果没超过则 先相减两数,然后加 24 小时2.如果大于0 相减两数*********************************************************/(temp.m_Hour - Right.m_Hour) 0 ? temp.m_Hour (temp.m_Hour - Right.m_Hour) 24 : temp.m_Hour temp.m_Hour - Right.m_Hour;b 0;//初始化 bool return temp;//返回临时变量}// 重载(类内重载;效率高,因为类内直接返回操作数本身减少编译时间)Time operator (TimeRight){*this *this Right;return *this;}
private:int m_Hour;//时针int m_minute;//分针int m_second;//秒针
};// 重载(全局重载;效率低,需要重新构造一个左操作数会增加编译时间)
//Time operator (TimeLeft, const TimeRight)
//{
// Left Left Right;
// return Left;
//}
void main()
{// 重载测试 结果 2:0:10Time t;Time t1(10, 20, 30);//设置初始时间10:20:30Time t2(15, 39, 40);//设置初始时间15:39:40t t1 t2;t.print();t1.print();t2.print();// - 重载测试 结果18:40:40cout endl;Time t3(9, 20, 20);Time t4(14, 39, 40);t t3 - t4;t.print();t3.print();t4.print();// 重载测试cout endl;t1 t2;t1.print();system(pause);
}运行结果
时间为2:0:10时间为17:39:50时间为18:40:40
请按任意键继续. . .