1

Possible Duplicate:
Operator overloading

"You are required to implement a class, called Duration, that represents a length of time, expressed in hours, minutes and seconds, for example the length of a track on a CD, or the time taken to run a marathon. The class should have an appropriate set of constructors (including error checking) and accessor methods. One of the constructors should permit the initialization of a Duration object using a single integer, representing the duration in seconds. The << and >> operators should be overloaded to allow the input and output of Duration objects via streams. A Duration object should be output in the format \h:mm:ss", for example \1:39:33", \0:09:07" and \1:00:08". Also the addition operator, +, should be overloaded so that two Duration objects can be added together to produce a new Duration object, and so that an integer number of seconds can be added to a Duration object. Lastly, the class should dene a type conversion from a Duration object to an integer representing the duration in seconds."

    #include <iostream>
    #include <stdlib.h>
    using namespace std;
    using std::cout;
    using std::cin;
    using std::ostream;
    using std::istream;

    class Duration 
    {
        private:
        int hours, minutes, seconds;
        int theSeconds;

        public:
        Duration() //default constructor.
        {}

        Duration(int hr, int min, int sec) //ordinary constructor.
        {
            hours = hr;
            minutes = min;
            seconds = sec;
        }

        inline int getHours()
        {
            return hours;
        }

        inline int getMinutes()
        {
            return minutes;
        }

        inline int getSeconds()
        {
            return seconds;
        }
    };

Hopefully i have gone in the right direction with this task so far(new this week to c++). However i am struggling with how to implement the part i have highlighted in bold and everything after that...

Please advise and help me. Also please note, this is NOT coursework etc. I'm just trying to prepare myself with c++ for next year. Thank you.

Community
  • 1
  • 1
binary101
  • 1,013
  • 3
  • 23
  • 34
  • Operator overloading in week 1?! Anyway, looking good so far, but what is your question? What have you tried? – Thomas Nov 01 '12 at 18:08
  • Hi. Yes haha. I'm trying to overload the >> and << operators in order to allow the input and output of Duration objects via streams... I've tried this: (although my code above has now changed since i tried) It keeps telling me the operator is binary and won't compile as it has 3 parameters?? I have no idea why... ostream& operator<<(ostream& os, const Duration& d) { os << "Time in Seconds: " << d.theSeconds << "\n"; return os; } – binary101 Nov 01 '12 at 18:13
  • Your code is not complete ... Show your overloading of the operator methods! – πάντα ῥεῖ Nov 01 '12 at 18:16
  • Show the code, not a summary, and not piecemeal. But as a guess, the code probably declares `operator<<` and `operator>>` as members. That would mean that they take three arguments: the object (of type `Duration`) that they're being called on, and the two named arguments. – Pete Becker Nov 01 '12 at 18:19
  • Note your `operator<<()` and `operator>>()` should probably not be member functions of the class... That's probably where the third parameter is coming from... – twalberg Nov 01 '12 at 18:19

1 Answers1

1

You should have a have std::ostream &-returning function implemented like so:

class Duration {

    int hours, minutes, seconds;
    int theSeconds;

    public:

        friend std::ostream & operator << (std::ostream & os, const Duration & dObj) {

            os << "The hours are " << dObj.hours << '\n';
            os << "The minutes are " << dObj.minutes << '\n';

            // ... and so on

        }

        Duration() {}
        Duration(int hr, int min, int sec) {
            hours = hr;
            minutes = min;
            seconds = sec;
    }

};

You would define operator>> in the same way as well:

friend std::istream & operator >> ( std::ostream & os, const Duration dObj ) {

    os >> dObj.minutes >> dObj.hours /* >> ... */;

    return os;

}

So you can use it like this:

Duration d;

std::cin >> d;

std::cout << d;
David G
  • 94,763
  • 41
  • 167
  • 253
  • Thank you very much for helping me. This is the direction i was trying to go. This implementation is excellent. And just so people are aware, i believe the additional parameter problem i was having is because i was trying to overload them inside the class. – binary101 Nov 01 '12 at 19:39