1. 程式人生 > >C++ Premier Plus 6th edition - Programming excercise - Chapter11 - 4

C++ Premier Plus 6th edition - Programming excercise - Chapter11 - 4

All altered

mytime3.h

// mytime3.h -- Time class with friends
#ifndef MYTIME3_H_
#define MYTIME3_H_
#include <iostream>

class Time
{
private:
    int hours;
    int minutes;
public:
    Time();
    Time(int h, int m = 0);
    void AddMin(int m);
    void AddHr(int h);
    void Reset(int
h = 0, int m = 0); // remove below 3 methods /* Time operator+(const Time & t) const; Time operator-(const Time & t) const; Time operator*(double n) const; */ // original body is:return t * m; // it uses this way is due to there is a operator* reload in member function above
// but have removed them, so that can not use this way anymore // however, created an friend operator* allows objective*double // so that here can still use return t * m; friend Time operator*(double m, const Time & t) { return t * m; // inline definition } friend std::ostream &
operator<<(std::ostream & os, const Time & t); // Newly added: use friends to reload operators friend Time operator+(const Time & t1, const Time & t2) ; friend Time operator-(const Time & t1, const Time & t2) ; friend Time operator*(const Time & t1, double m) ; }; #endif

mytime3.cpp

// mytime3.cpp  -- implementing Time methods
#include "mytime3.h"

Time::Time()
{
    hours = minutes = 0;
}

Time::Time(int h, int m )
{
    hours = h;
    minutes = m;
}

void Time::AddMin(int m)
{
    minutes += m;
    hours += minutes / 60;
    minutes %= 60;
}

void Time::AddHr(int h)
{
    hours += h;
}

void Time::Reset(int h, int m)
{
    hours = h;
    minutes = m;
}

std::ostream & operator<<(std::ostream & os, const Time & t)
{
    os << t.hours << " hours, " << t.minutes << " minutes";
    return os;
}

// Newly added
Time operator+(const Time & t1, const Time & t2)
{
    Time sum;

    // hours and minutes are int, no concerns of
    // (t1.minutes + t2.minutes) / 60 is less than 1
    sum.hours = t1.hours + t2.hours + (t1.minutes + t2.minutes) / 60;
    sum.minutes= (t1.minutes + t2.minutes) % 60;
    return sum;
}

Time operator-(const Time & t1, const Time & t2)
{
    Time abst;
    int min_left = (t1.hours * 60 + t1.minutes) - (t2.hours * 60 + t2.minutes);
    abst.hours = min_left / 60;
    abst.minutes = min_left % 60;
    return abst;
}

Time operator*(const Time & t1, double m)
{
    Time tim;
    int min_tot = (t1.hours * 60 + t1.minutes)*m;
    tim.hours = min_tot / 60;
    tim.minutes = min_tot % 60;
    return tim;
}

usetime3.cpp

//usetime3.cpp -- using the fourth draft of the Time class
// compile usetime3.cpp and mytime3.cpp together
#include <iostream>
#include "mytime3.h"

int main()
{
    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;     // operator+()
    cout << "Aida + Tosca: " << temp << endl;
    temp = aida* 1.17;  // member operator*()
    cout << "Aida * 1.17: " << temp << endl;
    cout << "10.0 * Tosca: " << 10.0 * tosca << endl;

	// newly added
	temp = aida - tosca;     // operator-()
	cout << "Aida - Tosca: " << temp << endl;

	std::cin.get();
    return 0; 
}