中易网

C++程序设计: 设计一个时间类Time,要求如下:

答案:2  悬赏:80  
解决时间 2021-03-23 22:27
C++程序设计:
设计一个时间类Time,要求如下:⑴:包含时(hour),分(minute),秒(second)私有数据成员。
⑵:包含构造函数,重载关于一段时间加上另一段时间的加法运算符“+”、重载关于一段时间减去另一段时间的减法运算符“-”、重载输入运算符“<<”和输出运算符“>>”
最佳答案
#include<iostream> #include<cstdlib> #include<string> using namespace std; class time { public: time() :h(0), m(0), s(0){} time(int newh, int newm, int news) :h(newh), m(newm), s(news){} int geth(){ return h; } int getm(){ return m; } int gets(){ return s; } void seth(int newh){ h = newh; } void setm(int newm){ m = newm; } void sets(int news){ s = news; } void display12(); private: int h, m, s; }; int main() { time t1(24, 4, 12), t2(2, 6, 6); t1.display12(); t2.display12(); system("pause"); return 0; } void time::display12() { string time; int h1; if (h > 12 && h != 24) { h1 = h - 12; time = "pm"; } else if (h == 24) { h1 = 0; time = "pm"; } else { h1 = h; time = "am"; } if (h1 / 10 == 0) cout << &apos;0&apos; << h1<<&apos;:&apos;; else cout << h1<<&apos;:&apos;; if (m / 10 == 0) cout << &apos;0&apos; << m << &apos;:&apos;; else cout << m << &apos;:&apos;; if (s / 10 == 0) cout << &apos;0&apos; << s; else cout << s; cout << time << endl; return; }
全部回答
#include<iostream> using namespace std; class Time {     public:     Time(void);     Time(int hour,int minute,int second);     Time operator +(const Time &time);     Time operator -(const Time &time);     friend ostream& operator <<(ostream& out,const Time &time);     friend istream& operator >>(istream& in,const Time &time);     private:     int hour;     int minute;     int second; }; Time::Time(void) {     this->hour = 0;     this->minute = 0;     this->second = 0;  } Time::Time(int hour,int minute,int second) {     this->hour = hour;     this->minute = minute;     this->second = second;  } Time Time::operator +(const Time &time) {     Time newTime;     newTime.hour = this->hour + time.hour;     newTime.minute = this->minute+ time.minute;     newTime.second = this->second + time.second;     if(newTime.second >= 60)     {         newTime.second %= 60;         newTime.minute += 1;     }      if(newTime.minute >= 60)     {         newTime.minute %= 60;         newTime.hour += 1;     }      if(newTime.hour >= 24)     {         newTime.hour %= 24;     }     return newTime; } Time Time::operator -(const Time &time) {     Time newTime;     newTime.hour = this->hour - time.hour;     newTime.minute = this->minute - time.minute;     newTime.second = this->second - time.second;     if(newTime.second < 0)     {         newTime.second += 60;         newTime.minute -= 1;     }      if(newTime.minute < 0)     {         newTime.minute += 60;         newTime.hour -= 1;     }      if(newTime.hour < 0)     {         newTime.hour += 24;     }     return newTime;     } ostream& operator <<(ostream& out,const Time &time) {     out<<time.hour<<":"<<time.minute<<":"<<time.second<<endl;     return out; } istream& operator >>(istream& in,const Time &time) {     in>>time.hour>>time.minute>>time.second;     return in; }
我要举报
如以上问答内容为低俗、色情、不良、暴力、侵权、涉及违法等信息,可以点下面链接进行举报!
大家都在看
小轿车上带电瓶车电池会不会有危险?
阎王爷嫁女儿。打一歇后语
sin a+sin 2a +sin 3a +...+sin na怎么求和?
今年9月3日全国放假1天有双薪吗
我是洛阳的想搞个兔子养殖厂就是不知道那种肉
两个月的巴哥犬老咔咔的咳怎么回事
做黄瓜酵素用告别的吗
拉威尔的《鹅妈妈组曲》中鹅妈妈讲了五个故事
捷达2015款 1.6l 手动豪华型怎样
不为别的,就为我们能永远在一起 这句话的英
托福听力是不是每个section都是10分钟答题,
描写春天景物的好句好段
韩束巨补水系列男士可以用吗?
9中室内羽毛球馆是怎么收费的,网要自己带吗
宝宝学步鞋什么牌子好,我宝宝现在10个月了准
推荐资讯
《我们最光耀的时刻》的英文演讲稿
C++虚函数到底需要实现吗
断骨增高方法可行不?
ATc音箱配什么功放
每天下午吃一块18g的德芙牛奶巧克力会胖吗?
怎么用PS把多张动图合成一张动图???
考研调剂 考研初试分比较低,但是能过国家线
儿童睡袋哪个牌子好
地租理论在中国的应用
武汉黑皮牛肉面加盟
求教合成火龙之心的方法?具体点谢谢。
脖子疼如何按摩
手机登qq时,显示手机磁盘不足,清理后重新登
刺客的套装怎么选啊?