时间类--运算符重载函数--gyy

2024-01-10 13:18

本文主要是介绍时间类--运算符重载函数--gyy,希望对大家解决编程问题提供一定的参考价值,需要的开发者们随着小编来一起学习吧!

/*
定义一个时间类Time,其成员如下:
有3个私有数据成员hour、min、sec,分别表示时间类中的时、分、秒
公有成员函数声明如下,函数体自己填写:
Time(int h, int m, int s);//带3个参数值的构造函数,注意时间是有规则的,注意在传递参数时如果遇到非法时间,则将hour、min、sec值都设为0 或要求重新输入
~Time();//析构函数
void  setTime(int h,int m,int s); //重新设置时间
void  showTime(); //显示时间
void  timeAdd(int h, int m ,int s);//进行时间相加,参数为时、分、秒
void  timeAdd(int s); //进行时间相加,参数为秒利用运算符重载函数作为友元函数来完成相应类对象的操作
friend Time operator+(Time t1,Time t2);   
friend Time operator+(Time t1,int s);friend bool operator>(Time t1,Time t2);
friend bool operator<(Time t1,Time t2);  
friend bool operator==(Time t1,Time t2); 
friend bool operator!=(Time t1,Time t2); friend ostream & operator<<(ostream & output,Time & t);
friend istream & operator>>(istream & input,Time & t);利用运算符重载函数作为成员函数来完成相应类对象的操作
Time operator++();  
Time operator++(int);在主函数中建立Time对象,通过对象调用成员函数来完成相应操作。
*/
#include <iostream.h>class Time
{public:Time(int h=0, int m=0, int s=0); //带3个参数值的构造函数,注意时间是有规则的,注意在传递参数时如果遇到非法时间,则将hour、min、sec值都设为0 或要求重新输入~Time();//析构函数void  setTime(int h,int m,int s); //重新设置时间void  showTime(); //显示时间void  timeAdd(int h, int m ,int s); //进行时间相加,参数为时、分、秒void  timeAdd(int s); //进行时间相加,参数为秒 Time operator++();   //对++进行运算符重载,作为对象的前置运算符Time operator++(int);  //对++进行运算符重载,作为对象的后置运算符friend Time operator+(Time t1,Time t2);  //对+进行运算符重载,完成对两个Time类对象的相加操作friend Time operator+(Time t1,int s);  //对+进行运算符重载,完成对1个Time类对象和整数的相加操作friend ostream & operator<<(ostream & output,Time & t); //对<<进行运算符重载,完成对Time类对象的输出操作friend istream & operator>>(istream & input,Time & t);  //对>>进行运算符重载,完成对Time类对象的输入操作friend bool operator>(Time t1,Time t2); //对>进行运算符重载,完成对两个Time类对象的大于比较操作friend bool operator<(Time t1,Time t2);  //对<进行运算符重载,完成对两个Time类对象的小于比较操作friend bool operator==(Time t1,Time t2);  //对==进行运算符重载,完成对两个Time类对象的等于比较操作friend bool operator!=(Time t1,Time t2);  //对!=进行运算符重载,完成对两个Time类对象的不等于比较操作private:int hour; int min;int sec;
};Time::Time(int h, int m, int s) //带3个参数值的构造函数,注意时间是有规则的,注意在传递参数时如果遇到非法时间,则将hour、min、sec值都设为0 或要求重新输入
{hour=(h>=0&&h<24)?h:0;min=(m>=0&&m<60)?m:0;sec=(s>=0&&s<60)?s:0;
}Time:: ~Time()//析构函数
{
}void  Time::setTime(int h,int m,int s) //重新设置时间
{hour=(h>=0&&h<24)?h:0;min=(m>=0&&m<60)?m:0;sec=(s>=0&&s<60)?s:0;
}void  Time::showTime()  //显示时间
{cout<<hour<<":"<<min<<":"<<sec<<endl;
}void  Time::timeAdd(int h, int m ,int s) //进行时间相加,参数为时、分、秒
{if(h<0||h>=24||m<0||m>=60||s<0||s>=60){cout<<"时间参数有误,计算时间相加不成功"<<endl;}else{cout<<hour<<":"<<min<<":"<<sec;hour=(hour+h+(min+m+(sec+s)/60)/60)%24;min=(min+m+(sec+s)/60)%60;sec=(sec+s)%60;cout<<"+"<<h<<":"<<m<<":"<<s<<"="<<hour<<":"<<min<<":"<<sec<<endl;}    
}void  Time::timeAdd(int s) //进行时间相加,参数为秒 
{cout<<hour<<":"<<min<<":"<<sec;hour=(hour+(min+(sec+s)/60)/60)%24;min=(min+(sec+s)/60)%60;sec=(sec+s)%60;cout<<"+"<<s<<"="<<hour<<":"<<min<<":"<<sec<<endl;
}Time Time::operator++()  //对++进行运算符重载,作为对象的前置运算符
{ hour=(hour+(min+(sec+1)/60)/60)%24;min=(min+(sec+1)/60)%60;sec=(sec+1)%60;return *this;  //返回改变后的对象
}Time Time::operator++(int)  //对++进行运算符重载,作为对象的后置运算符
{Time temp=*this;hour=(hour+(min+(sec+1)/60)/60)%24;min=(min+(sec+1)/60)%60;sec=(sec+1)%60;return temp;  //返回改变前的对象
}Time operator+(Time t1,Time t2)//对+进行运算符重载,完成对两个Time类对象的相加操作
{Time t3;t3.hour=(t1.hour+t2.hour+(t1.min+t2.min+(t1.sec+t2.sec)/60)/60)%24;t3.min=(t1.min+t2.min+(t1.sec+t2.sec)/60)%60;t3.sec=(t1.sec+t2.sec)%60;return t3;
}Time operator+(Time t1,int s) //对+进行运算符重载,完成对1个Time类对象和整数的相加操作
{Time t2;t2.hour=(t1.hour+(t1.min+(t1.sec+s)/60)/60)%24;t2.min=(t1.min+(t1.sec+s)/60)%60;t2.sec=(t1.sec+s)%60;return t2;
}ostream & operator<<(ostream & output,Time & t) //对<<进行运算符重载,完成对Time类对象的输出操作
{output<<t.hour<<":"<<t.min<<":"<<t.sec<<endl;return output;
}istream & operator>>(istream & input,Time & t) //对>>进行运算符重载,完成对Time类对象的输入操作
{cout<<"input the hour :";input>>t.hour;cout<<"input the min :";input>>t.min;cout<<"input the sec:";input>>t.sec;return input;
}bool operator>(Time t1,Time t2) //对>进行运算符重载,完成对两个Time类对象的大于比较操作{if(t1.hour>t2.hour)return true;if(t1.hour==t2.hour&&t1.min>t2.min)return true;if(t1.hour==t2.hour&&t1.min==t2.min&&t1.sec>t2.sec)return true;return false;}bool operator<(Time t1,Time t2) //对<进行运算符重载,完成对两个Time类对象的小于比较操作{if(t1.hour<t2.hour)return true;if(t1.hour==t2.hour&&t1.min<t2.min)return true;if(t1.hour==t2.hour&&t1.min==t2.min&&t1.sec<t2.sec)return true;return false;}bool operator==(Time t1,Time t2) //对==进行运算符重载,完成对两个Time类对象的等于比较操作{if(t1.hour==t2.hour&&t1.min==t2.min&&t1.sec==t2.sec)return true;elsereturn false;  }bool operator!=(Time t1,Time t2) //对!=进行运算符重载,完成对两个Time类对象的不等于比较操作{if(t1.hour==t2.hour&&t1.min==t2.min&&t1.sec==t2.sec)return false;elsereturn true;  }int main()
{Time t1(1,2,3);   //定义时间对象cout<<"t1=";t1.showTime();    //显示时间信息t1.timeAdd(2,3,4);   //进行分、时、秒的相加操作cout<<"t1=";t1.showTime();    //显示时间信息t1.timeAdd(22,59,4);  //进行分、时、秒的相加操作cout<<"t1=";t1.showTime();   //显示时间信息t1.timeAdd(155);  //进行秒的相加操作cout<<"t1=";t1.showTime();   //显示时间信息Time t2(25,62,-9); //定义时间对象cout<<"t2=";t2.showTime(); //显示时间信息cout<<endl;t2=t1++;   //++运算符重载函数调用,对象后置运算符cout<<"after  t2=t1++"<<endl;cout<<"t1=";t1.showTime();cout<<"t2=";t2.showTime();t2=++t1; //++运算符重载函数调用,对象前置运算符cout<<"after  t2=++t1"<<endl;cout<<"t1=";t1.showTime();cout<<"t2=";t2.showTime();Time t3;t3=t1+t2;  //+运算符重载函数调用,完成两个对象相加cout<<"after  t3=t1+t2"<<endl;cout<<"t3=";t3.showTime();t3=t3+100; //+运算符重载函数调用,完成一个对象和秒的相加cout<<"after  t3=t3+100"<<endl;cout<<"t3=";t3.showTime();cout<<"input t1 again"<<endl;cin>>t1;          //>>运算符重载函数调用,完成对象输入cout<<"t1="<<t1;  //<<运算符重载函数调用,完成对象输出cout<<"input t2 again"<<endl;cin>>t2;         //>>运算符重载函数调用,完成对象输入cout<<"t2="<<t2; //<<运算符重载函数调用,完成对象输出if(t1>t2)     //>运算符重载函数调用,完成对象比较cout<<"t1>t2"<<endl;if(t1<t2)     //<运算符重载函数调用,完成对象比较cout<<"t1<t2"<<endl;if(t1==t2)    //==运算符重载函数调用,完成对象比较cout<<"t1==t2"<<endl;if(t1!=t2)    //!=运算符重载函数调用,完成对象比较cout<<"t1!=t2"<<endl;return 0;
}




这篇关于时间类--运算符重载函数--gyy的文章就介绍到这儿,希望我们推荐的文章对编程师们有所帮助!



http://www.chinasem.cn/article/590936

相关文章

服务器集群同步时间手记

1.时间服务器配置(必须root用户) (1)检查ntp是否安装 [root@node1 桌面]# rpm -qa|grep ntpntp-4.2.6p5-10.el6.centos.x86_64fontpackages-filesystem-1.41-1.1.el6.noarchntpdate-4.2.6p5-10.el6.centos.x86_64 (2)修改ntp配置文件 [r

hdu1171(母函数或多重背包)

题意:把物品分成两份,使得价值最接近 可以用背包,或者是母函数来解,母函数(1 + x^v+x^2v+.....+x^num*v)(1 + x^v+x^2v+.....+x^num*v)(1 + x^v+x^2v+.....+x^num*v) 其中指数为价值,每一项的数目为(该物品数+1)个 代码如下: #include<iostream>#include<algorithm>

C++操作符重载实例(独立函数)

C++操作符重载实例,我们把坐标值CVector的加法进行重载,计算c3=c1+c2时,也就是计算x3=x1+x2,y3=y1+y2,今天我们以独立函数的方式重载操作符+(加号),以下是C++代码: c1802.cpp源代码: D:\YcjWork\CppTour>vim c1802.cpp #include <iostream>using namespace std;/*** 以独立函数

函数式编程思想

我们经常会用到各种各样的编程思想,例如面向过程、面向对象。不过笔者在该博客简单介绍一下函数式编程思想. 如果对函数式编程思想进行概括,就是f(x) = na(x) , y=uf(x)…至于其他的编程思想,可能是y=a(x)+b(x)+c(x)…,也有可能是y=f(x)=f(x)/a + f(x)/b+f(x)/c… 面向过程的指令式编程 面向过程,简单理解就是y=a(x)+b(x)+c(x)

MiniGPT-3D, 首个高效的3D点云大语言模型,仅需一张RTX3090显卡,训练一天时间,已开源

项目主页:https://tangyuan96.github.io/minigpt_3d_project_page/ 代码:https://github.com/TangYuan96/MiniGPT-3D 论文:https://arxiv.org/pdf/2405.01413 MiniGPT-3D在多个任务上取得了SoTA,被ACM MM2024接收,只拥有47.8M的可训练参数,在一张RTX

批处理以当前时间为文件名创建文件

批处理以当前时间为文件名创建文件 批处理创建空文件 有时候,需要创建以当前时间命名的文件,手动输入当然可以,但是有更省心的方法吗? 假设我是 windows 操作系统,打开命令行。 输入以下命令试试: echo %date:~0,4%_%date:~5,2%_%date:~8,2%_%time:~0,2%_%time:~3,2%_%time:~6,2% 输出类似: 2019_06

利用matlab bar函数绘制较为复杂的柱状图,并在图中进行适当标注

示例代码和结果如下:小疑问:如何自动选择合适的坐标位置对柱状图的数值大小进行标注?😂 clear; close all;x = 1:3;aa=[28.6321521955954 26.2453660695847 21.69102348512086.93747104431360 6.25442246899816 3.342835958564245.51365061796319 4.87

OpenCV结构分析与形状描述符(11)椭圆拟合函数fitEllipse()的使用

操作系统:ubuntu22.04 OpenCV版本:OpenCV4.9 IDE:Visual Studio Code 编程语言:C++11 算法描述 围绕一组2D点拟合一个椭圆。 该函数计算出一个椭圆,该椭圆在最小二乘意义上最好地拟合一组2D点。它返回一个内切椭圆的旋转矩形。使用了由[90]描述的第一个算法。开发者应该注意,由于数据点靠近包含的 Mat 元素的边界,返回的椭圆/旋转矩形数据

【MRI基础】TR 和 TE 时间概念

重复时间 (TR) 磁共振成像 (MRI) 中的 TR(重复时间,repetition time)是施加于同一切片的连续脉冲序列之间的时间间隔。具体而言,TR 是施加一个 RF(射频)脉冲与施加下一个 RF 脉冲之间的持续时间。TR 以毫秒 (ms) 为单位,主要控制后续脉冲之前的纵向弛豫程度(T1 弛豫),使其成为显著影响 MRI 中的图像对比度和信号特性的重要参数。 回声时间 (TE)

Unity3D 运动之Move函数和translate

CharacterController.Move 移动 function Move (motion : Vector3) : CollisionFlags Description描述 A more complex move function taking absolute movement deltas. 一个更加复杂的运动函数,每次都绝对运动。 Attempts to