8_2時間類中的運算子過載
#include <iostream> using namespace std; class CTime { private: unsigned short int hour; // 時 unsigned short int minute; // 分 unsigned short int second; // 秒 public: CTime(int h=0,int m=0,int s=0); void setTime(int h,int m,int s); void display(); //比較運算子(二目)的過載 bool operator > (CTime &t); bool operator < (CTime &t); bool operator >= (CTime &t); bool operator <= (CTime &t); bool operator == (CTime &t); bool operator != (CTime &t); //二目運算子的過載 CTime operator+(CTime &c);//返回c所規定的時、分、秒後的時間,例t1(8,20,25),t2(11,20,50),t1+t2為:41:15 CTime operator-(CTime &c);//對照+理解 CTime operator+(int s);//返回s秒後的時間 CTime operator-(int s);//返回s秒前的時間 //一目運算子的過載 CTime operator++(int);//後置++,下一秒 CTime operator++();//前置++,下一秒 CTime operator--(int);//後置--,前一秒 CTime operator--();//前置--,前一秒 //賦值運算子的過載 CTime operator+=(CTime &c); CTime operator-=(CTime &c); CTime operator+=(int s);//返回s秒後的時間 CTime operator-=(int s);//返回s秒前的時間 }; //建構函式 CTime::CTime(int h,int m,int s) { hour=h; minute=m; second=s; } // 設定時間 void CTime::setTime(int h,int m,int s) { hour=h; minute=m; second=s; } //顯示時間 void CTime::display() { cout<<hour<<':'<<minute<<':'<<second<<endl; } //比較運算子的過載 bool CTime::operator > (CTime &t) // 判斷時間t1>t2 { if (hour>t.hour) return true; if (hour<t.hour) return false; if (minute>t.minute) return true; if (minute<t.minute) return false; if (second>t.second) return true; return false; } bool CTime::operator < (CTime &t)// 判斷時間t1<t2 { if (hour<t.hour) return true; if (hour>t.hour) return false; if (minute<t.minute) return true; if (minute>t.minute) return false; if (second<t.second) return true; return false; } bool CTime::operator == (CTime &t)// 判斷時間t1==t2 { if (*this<t || *this>t) return false; return true; } bool CTime::operator != (CTime &t) // 判斷時間t1!=t2 { if (*this==t) return false; return true; } bool CTime::operator >= (CTime &t)// 判斷時間t1>=t2 { if (*this<t) return false; return true; } bool CTime::operator <= (CTime &t) // 判斷時間t1<=t2 { if (*this>t) return false; return true; } //二目運算子的過載 // 計算時間之和, 返回c所規定的時、分、秒後的時間,例t1(8,20,25),t2(11,20,50),t1+t2為:41:15 CTime CTime::operator + (CTime &t) { int h,m,s; s=second+t.second; m=minute+t.minute; h=hour+t.hour; if (s>59) { s-=60; m++; } if (m>59) { m-=60; h++; } if (h>23) h-=24; CTime t0(h,m,s); return t0; } //返回s秒後的時間 CTime CTime::operator+(int s) { int ss=s%60; int mm=(s/60)%60; int hh=s/3600; CTime t0(hh,mm,ss); return *this+t0; } // 計算時間之差 CTime CTime::operator - (CTime &t) { int h,m,s; s=second-t.second; m=minute-t.minute; h=hour-t.hour; if (s<0) { s+=60; m--; } if (m<0) { m+=60; h--; } if (h<0) h+=24; CTime t0(h,m,s); return t0; } //返回s秒前的時間 CTime CTime::operator-(int s) { int ss=s%60; int mm=(s/60)%60; int hh=s/3600; CTime t0(hh,mm,ss); return *this-t0; } //一目運算子的過載 CTime CTime::operator++(int)//後置++,下一秒 { CTime t=*this; *this=*this+1; return t; } CTime CTime::operator++()//前置++,下一秒 { *this=*this+1; return *this; } CTime CTime::operator--(int)//後置--,前一秒 { CTime t=*this; *this=*this-1; return t; } CTime CTime::operator--()//前置--,前一秒 { *this=*this-1; return *this; } //賦值運算子的過載 CTime CTime::operator+=(CTime &c) { *this=*this+c; return *this; } CTime CTime::operator-=(CTime &c) { *this=*this-c; return *this; } CTime CTime::operator+=(int s)//返回s秒後的時間 { *this=*this+s; return *this; } CTime CTime::operator-=(int s)//返回s秒前的時間 { *this=*this-s; return *this; } int main() { CTime t1(8,20,25),t2(11,20,50),t; cout<<"t1為:"; t1.display(); cout<<"t2為:"; t2.display(); cout<<"下面比較兩個時間大小:\n"; 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; if (t1>=t2) cout<<"t1≥t2"<<endl; if (t1<=t2) cout<<"t1≤t2"<<endl; cout<<endl; //在測試下面的程式碼時,採用單步執行的方法跟蹤 t=t1+t2; t.display(); t=t1-t2; t.display(); t=t1+10; t.display(); t=t1-10; t.display(); return 0; }
相關推薦
8_2時間類中的運算子過載
#include <iostream> using namespace std; class CTime { private: unsigned short int hour; // 時 unsigned short int minute; // 分
YTUOJ——C++時間類的運算子過載
題目描述 C++時間類的運算子過載 定義一個時間類Time,其資料成員為表示時間的小時(hour)、分(minute),秒(second)。 過載運算子“+”,使之能用於時間物件的加法運算;過載運算子“<<”,使之能用於時間物件的輸出操作。 (1)參加運算的兩個運算元可以都是時間
《C++第九周實驗報告3-1》----接第8周任務3,定義分數類中運算子過載,實現分數的輸入輸出
/* (程式頭部註釋開始) * 程式的版權和版本宣告部分 * Copyright (c) 2011, 煙臺大學計算機學院學生 * All rights reserved. * 檔名稱: CFraction.cpp *
string類中運算子過載的實現
#include<iostream> using namespace std; class MyString { public: MyString(); MyString(const int number); MyStri
類中的過載操作符(=)和copy建構函式的區別
過載操作符的關鍵字? copy建構函式? 過載“”=“”? 兩種方式的使用方法有什麼不同? lhs和rhs變數的含義?經常在哪裡面見到? lhs指的是==運算子左邊的運算元;(left-hand side) rhs指的是==運算子右邊的運算元;(righht-han
作業11: 類_運算子過載
作業11: 類_運算子過載1.設向量X = ( x1 ,x2 ,x3) 和 Y = ( y1 , y2 ,y3 ),則它們之間的加、減和積分別定義為:X + Y = ( x1 + y1 , x2 + y2 , x3 + y3 ) X - Y = ( x1 - y1 , x2
2014第八週專案三--分數類的運算子過載
/* *程式的版權和版本宣告部分: *Copyright(c)2014,煙臺大學計算機學院學生 *All rights reserved. *檔名稱: *作者:劉曉曉 *完成日期:2014年 04月15號 *版本號:v1.0 *對任務及求解方法的描述部分: *輸入描述: 無
C#中運算子過載的幾點注意
這是一篇簡記,因此不做特別的排版 1、運算子過載不能多型 這是最容易出問題的地方,看下面的程式碼 過載者如下: public class Father { public int value; public static implicit operat
8_3分數類的運算子過載
#include <iostream> using namespace std; class CFraction {private: int nume; // 分子 int deno;
多型性-類成員運算子過載
1、如果運算子作為類的成員函式過載,其引數個數要比該運算子實際引數個數少一個。其第一個引數是通過物件的this指標傳遞的,this指標是一個隱含的引數,有c++編譯系統自行處理。而靜態成員函式沒有this指標,所以不能將運算子過載為類的靜態成員函式。2、二元運算子
複數類的運算子過載
#include <iostream> using namespace std; class Complex { public: Complex(){real=0;imag=0;} Complex(double r,dou
[c++]String字串類的運算子過載
在c++中有一個新定義的型別string,可以不用那麼麻煩的操作字串,並且一些高階的運算子過載讓她的使用更加便捷 下面是String類的定義和成員函式的定義: #ifndef operator_operator_h #define operator_operator_h
類,運算子過載,this指標
class Complex { private : double m_real; double m_imag; public: // 無引數建構函式 // 如果建立一個類你沒有寫任何建構函式,則系統會自動
過載運算子(時間類友元)
#include using namespace std; class Time { private: int hour; int minute; int second; public: Time(int a=0,int b=0,int c=0) { this->hour=a; th
過載運算子++的應用(時間類)
#include using namespace std; class Time { private: int hour; int minute; int second; public: Time(int a=0,int b=0,int c=0) { this->hour=a; th
關於模板類中輸入輸出運算子的過載
當輸入輸出運算子在標頭檔案中寫時,如果沒有#include <iostream>和using namespace std;程式將會有100多個bug;當輸入輸出運算子過載為模板類的友元函式時,如果只是在類體中寫為ostream& operator&
請編寫程式,處理一個複數與一個double數相加的運算,結果存放在一個double型的變數d1中,輸出d1的值,再以複數形式輸出此值。定義Complex(複數)類,在成員函式中包含過載型別轉換運算子:
#include <iostream> #include <iomanip> using namespace std; class Complex { public: Complex(); Complex(double r
C ++中字串類運算子過載示例
一、基本概念 (一) 函式過載的含義 所謂過載,就是重新賦予新的含義。函式過載就是對一個已有的函式賦予新的含義,使之實現新功能,因此,一個函式名就可以用來代表不同功能的函式,也就是”一名多用”。 (二) 為什麼要進行函式過載 一般情況下,編譯器對現有操作符的運算元是有一定的限制,但是
9.3分數類中的運算子過載,完成分數的加減乘除
/* * Copyright (c) 2014, 煙臺大學計算機學院 * All rights reserved. * 作 者:王穎 * 完成日期:2014 年 4 月 14 日 * 版 本 號:v1.0 * 輸入描述: 無 * 問題描述:分數類中的運算子過載,在分數
Time類中的運算子過載(2)-二目運算子的過載
問題描述及程式碼: /* *copyright (c) 2016,煙臺大學計算機學院 *All rights reserved. *檔名稱:hellow.cpp *作者:田甜 *完成日期:2016年5月19日 *版本號:v1.0 * *問題描述:實現Time類中的