#h文件
#include <iostream>
class time
{
int minute;
int hour;
public :
time ( ) { minute = 0 , hour = 0 ; } ;
time ( int a, int b) { hour = a, minute = b; } ;
time ( int a) { hour = a / 60 , minute = a - hour * 60 ; } ;
friend time operator + ( const time& a, const time& b) ;
friend time operator - ( const time& a, const time& b) ;
friend time operator * ( const time& a, double b) ;
friend time operator * ( double b, const time& a) { return a * b; } ;
friend std:: ostream & operator << ( std:: ostream & os, time & a) ;
~ time ( ) ;
} ;
```定义函数
#include "time.h"
time operator + ( const time& a, const time& b)
{
time c;
int all;
all = ( a. hour * 60 + a. minute + b. hour * 60 + b. minute) ;
c. hour = all/ 60 ;
c. minute = all - c. hour * 60 ;
return c;
}
time operator - ( const time& a, const time& b)
{
time c;
int all;
all = ( a. hour * 60 + a. minute - b. hour * 60 - b. minute) ;
c. hour = all / 60 ;
c. minute = all - c. hour * 60 ;
return c;
}
time operator * ( const time& a, double b)
{
time c;
double all;
all = ( a. hour * 60 + a. minute) ;
all * = b;
c. hour = all/ 60 ;
c. minute = all - c. hour * 60 ;
return c;
}
std:: ostream & operator << ( std:: ostream & os, time & a)
{
os << "hour:" << a. hour << "," << "minute:" << a. minute;
return os;
}
time:: ~ time ( )
{
}
```测试
#include <iostream>
#include "time.h"
using namespace std;
int main ( )
{
time test;
cout << test << endl;
time test1 ( 2 , 10 ) ;
time test10 ( 4 , 20 ) ;
cout << test1 << endl;
time test2 ( 100 ) ;
cout << test2 << endl;
time test3;
test3 = test1 + 100 ;
cout << test3 << endl;
time test4 = test1 - 200 ;
cout << test4 << endl;
time test5 = test1 - test10;
cout << test5 << endl;
time test6 = test1* 0.6 ;
cout << test6 << endl;
test6 = 0.5 * test1;
cout << test6 << endl;
system ( "pause" ) ;
return 0 ;
}