Код:
#ifndef MYTIME3_H_
#define MYTIME3_H_
#include <iostream>
class Time
{
private:
...
public:
...
...
//friends
friend Time operator+(const Time & t1, const Time & t2);
friend Time operator-(const Time & t1, const Time & t2);
friend Time operator*(double m, const Time & t)
{
return t * m;
}
friend Time operator*(const Time & t, double mult);
friend std::ostream & operator<<(std::ostream & os, const Time & t);
};
****************
#include "mytime3.h"
Time::Time()
{
}
Time::Time(int h, int m )
{
}
void Time::AddMin(int m)
{
}
void Time::AddHr(int h)
{
}
void Time::Reset(int h, int m)
{
...
}
Time operator+(const Time & t1, const Time & t2)
{
int m = t1.minutes + t2.minutes;
int op_m = m % 60;
int h = t1.hours + t2.hours + m / 60;
Time sum(h, op_m);
return sum;
}
Time operator-(const Time & t1, const Time & t2)
{
int st1 = (t1.hours * 60) + t1.minutes;
int st2 = (t2.hours * 60) + t2.minutes;
int h = (st1 - st2) / 60;
int m = (st1 - st2) % 60;
Time sum(h, m);
return sum;
}
Time operator*(const Time & t, double mult);
{
Time result;
long totalminutes = t.hours * mult * 60 + t.minutes * mult;
result.hours = totalminutes / 60;
result.minutes = totalminutes % 60;
return result;
}
std::ostream & operator<<(std::ostream & os, const Time & t)
{
...
}
**********
#include <iostream>
#include "mytime3.h"
using namespace std;
int main(int argc, char *argv[])
{
using std::cout;
using std::endl;
Time aida(3, 35);
Time tosca(2, 48);
Time temp;
cout << "Aida and Tosca:\n";
cout << aida<<"; " << tosca << endl;
temp = aida + tosca;
cout << "Aida + Tosca: " << temp << endl;
temp = aida * 1.17;
cout << "Aida * 1.17: " << temp << endl;
cout << "10 * Tosca: " << 10 * tosca << endl;
temp = aida - tosca;
cout<<"Aida - Tosca:"<< temp << endl;
system("PAUSE");
return EXIT_SUCCESS;
}