Thread: overload operator<

  1. #1
    Unregistered
    Guest

    overload operator<

    Hello everyone,
    I am trying to figure out how to OVERLOAD the comparision operator< to compare two time objects.
    What are the 2 time objects?And how do I know where to begin with the overload?
    Thanks for the help,
    Janet


    #ifndef TIME6_H
    #define TIME6_H

    class Time {
    public:
    Time( int = 0, int = 0, int = 0 );

    // set functions
    Time &setTime( int, int, int );
    Time &setHour( int );
    Time &setMinute( int );
    Time &setSecond( int );


    int getHour() const;
    int getMinute() const;
    int getSecond() const;


    void printMilitary() const;
    void printStandard() const;
    private:
    int hour;
    int minute;
    int second;
    };

    #endif

  2. #2
    Unregistered
    Guest
    You can make a single parameter version as a public member function or a two parameter version as a public friend function. Within the body of the function you can do something like this:

    If time1.hour < time2.hour
    time one is earlier than time 2
    else if time1.hour == time2.hour && time1.min < time2.min
    timeone is earlier than time2
    else if time1.hour == time2.hour && time1.min == time2.min && time1.sec < time2.sec
    time one is earlier than time 2
    else
    time on is not earlier than time2.

  3. #3
    Unregistered
    Guest
    Thanksfor the help, but I am trying to figure if out what is the function prototype and function definition? I thought the function prototypes was some thing like

    int box (int);

  4. #4
    Unregistered
    Guest
    Well, if you use the one parameter, public method version the prototype could be :

    bool operator <(Time &);

    with definition starting as:
    bool Time::operator< (Time & rhs)
    {
    //whatever
    }

    and if you use the friend two parameter version it could be :

    friend bool operator<(Time &, Time &);

    with definition

    bool operator<(Time & lhs, Time & rhs)
    {
    //whtever

    The public method vesrion assumes that the left hand side instance of Time is the calling object, whereas the friend version can't make that distinction and needs to know both Time objects you are trying to compare.

    The return type could be anything you like, but if your compiler allows type bool that is a common return type.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. overload *
    By shuo in forum C++ Programming
    Replies: 5
    Last Post: 06-10-2007, 04:44 AM
  2. Overload Operators
    By verbity in forum C++ Programming
    Replies: 3
    Last Post: 03-25-2007, 11:13 AM
  3. Having trouble with operator*=()
    By Lurker in forum C++ Programming
    Replies: 10
    Last Post: 10-26-2003, 03:03 PM
  4. overload new and delete
    By Roaring_Tiger in forum C++ Programming
    Replies: 2
    Last Post: 07-30-2003, 07:48 PM
  5. C++ Operator Overload Question
    By cworld in forum C++ Programming
    Replies: 0
    Last Post: 03-25-2002, 07:17 PM