Chaz
Chaz

Reputation: 205

Using overloaded operators on times

I have been given this half written class.

using namespace std;

#include <iostream>
#include <iomanip>
#include "Time.h"

Time::Time()
{ hour = min = sec = 0;
}

Time::Time(int h, int m, int s)
{ setTime(h, m, s);
}

void Time::setTime(int h, int m, int s)
{ hour = (h>=0 && h<24) ? h : 0;
  min = (m>=0 && m<60) ? m : 0;
  sec = (s>=0 && s<60) ? s : 0;
}

Time& Time::operator+=(unsigned int n)
{ sec += n;
  if (sec >= 60)
  { min += sec/60;
    sec %= 60;
    if (min >=60)
    { hour = (hour + min/60) % 24;
      min %= 60;
    }
  }
  return *this;
}

Time Time::operator+(unsigned int n) const
{ Time tCopy(*this);
  tCopy += n;
  return tCopy;
}

Time& Time::operator++()        // prefix version
{ *this += 1;
  return *this;
}

Time Time::operator++(int n)    // postfix version
{ Time tCopy(*this);
  *this += 1;
  return tCopy;
}

ostream& operator<<(ostream &o, const Time &t)
{ o << setfill('0') << setw(2) <<  t.hour << ':' << setw(2) << t.min << ':' << setw(2) << t.sec;
  return o;
}

and this header file,

// using _TIMEX_H_ since _TIME_H_ seems to be used by some C++ systems

#ifndef _TIMEX_H_
#define _TIMEX_H_

using namespace std;

#include <iostream>

class Time
{ public:
    Time();
    Time(int h, int m = 0, int s = 0);
    void setTime(int, int, int);
    Time operator+(unsigned int) const;
    Time& operator+=(unsigned int);
    Time& operator++();    // postfix version
    Time operator++(int);  // prefix version

    // new member functions that you have to implement

    Time operator-(unsigned int) const;
    Time& operator-=(unsigned int);
    Time& operator--();      // postfix version
    Time operator--(int);  // prefix version

    bool operator==(const Time&) const;
    bool operator<(const Time&) const;
    bool operator>(const Time&) const;

  private:
    int hour, min, sec;

  friend ostream& operator<<(ostream&, const Time&);

  // new friend functions that you have to implement

  friend bool operator<=(const Time&, const Time&);
  friend bool operator>=(const Time&, const Time&);
  friend bool operator!=(const Time&, const Time&);

  friend unsigned int operator-(const Time&, const Time&);
};

#endif

I think I can implement the other operator functions and the friend functions but I'm having trouble writing a main method to test how this currently works, I've only just started learning C++ and I'm really struggling with it so apologies for not having any idea on how to write this. Thanks in advance.

Upvotes: 1

Views: 2168

Answers (1)

Saurabh
Saurabh

Reputation: 60

There is nothing special in testing the overloaded operators. Just make 2 objects in main and write something like this:

Time t1(10,20,30),t2(1,2,3);

Now,for checking += operator,write:

t1+=10;

Your overloaded function will be called automatically,you can also add 2 objects instead of a number,like this:

t1+=t2;

The only difference will be the parameter,instead of a int,it will be an object of type Time.

For checking the << operator,simply write:

cout<<t1;

and the rest will be handled by the friend function.

Hope this helps.

Upvotes: 1

Related Questions