Thread: Function overloading

  1. #1
    Registered User
    Join Date
    Sep 2009
    Posts
    68

    Function overloading

    Working with overloading unary, binary, etc etc operators

    We are using classes to do this, based on Times.

    My << operator is suppose to output each Data Member of the class while calling a reduce function. However everything is required to be a const so my reduce function can't change it.

    My second issue is with my != function. I may be missing the obvious but I can't get it to work.

    Below are my cpp file with function definitions and header file with the class and function prototypes.

    Code:
    Time.cpp
    
    #include "Time.h"
    #include <iostream>
    
    using namespace std;
    
    ostream& operator << (ostream& ostr, const Time& rhs)
    {
        ostr << rhs.Hour.Reduce() << ":" << rhs.Minute.Reduce() << ":" << rhs.Second.Reduce();
        return ostr;
    }
    
    istream& operator >> (istream& istr,  Time& rhs)
    {
       istr >> rhs.Hour >> rhs.Minute >> rhs.Second;
       return istr;
    }
    
    Time::Time()
    {
    	Hour = 0;
            Minute = 0;
            Second = 0;
    }
    
    Time::Time(int H, int M, double S)
    {
       Hour = H;
       Minute = M;
       Second = S;
    }
    
    Time::Time(const Time& V)
    {
    	Hour = V.Hour;
    	Minute = V.Minute;
    	Second = V.Second;
    }
    
    const Time Time::operator + (const Time& rhs) const
    {
    	return  Time( Hour + rhs.Hour, Minute + rhs.Minute, Second + rhs.Second);
    }
    
    const Time Time::operator - (const Time& rhs) const
    {
      return operator + ( -rhs );
    
    }
    
    const Time Time:: operator - () const
    {
        return Time(-Hour,Minute,Second);
    }
    
    const Time Time::operator + () const
    {
       return *this;
    }
    
    const bool Time::operator < (const Time& rhs) const
    {
    	return (Hour < rhs.Hour) || (Hour <= rhs.Hour && Minute < rhs.Minute)
    		|| (Hour <= rhs.Hour && Minute <= rhs.Minute && Second < rhs.Second);
    }
    
    const bool Time::operator == (const Time& rhs) const
    {
      return (Hour == rhs.Hour && Minute == rhs.Minute && Second == rhs.Second);
    }
    
    const bool Time::operator <= (const Time& rhs) const
    {
      return operator < (rhs) || operator == (rhs );
    }
    
    const bool Time::operator > (const Time& rhs) const
    {
      return !(operator <= (rhs) ) ;
    }
    
    const bool Time::operator >= (const Time& rhs) const
    {
      return operator > (rhs) || operator == (rhs) ;
    }
    
    
    const Time Time::operator = (const Time& rhs)
    {
        Hour = rhs.Hour;
        Minute = rhs.Minute;
        Second = rhs.Second;
        return rhs;
    }
    
    const bool Time::operator != (const Time& rhs) const
    {
        return !(operator == rhs());
    }
    
    const Time Time::operator ++ ()
    {
        operator = (operator + (Time(1,1,1)));
        return *this;
    }
    
    const Time Time::operator ++ (int)
    {
       Time Tmp(*this);
       operator = ( operator + (Time(1,1,1))) ;
       return Tmp;
    }
    
    
    void Time::Reduce()
    {
    	int count;
    
    	count = static_cast <int> (Second) / 60;
    	Second = static_cast <int> (Second) % 60;
    	Minute += count;
    
    	count = Minute / 60;
    	Minute = Minute % 60;
    	Hour += count;
    
    	Hour = Hour % 24;
    }
    Code:
    Time.h
    
    #include <iostream>
    
    using namespace std;
    
    class Time
    {
    private:
           int Hour;
           int Minute;
           int Second;
           void Reduce();
    public:
    	   Time();
    	   Time(int H, int M, double S);
    	   Time(const Time& V);
    	   const Time operator + (const Time& rhs) const ;  //Add two times
    	   const Time operator - (const Time& rhs) const ;  //sub two times
    	   const bool operator < (const Time& rhs) const;  // < two times
               const bool operator == (const Time& rhs) const;  // == two times
    	   const bool operator <= (const Time& rhs) const;  // <= two times
    	   const bool operator > (const Time& rhs) const;  // > two times
    	   const bool operator >= (const Time& rhs) const;  // > two times
               const bool operator != (const Time& rhs) const;
    	   const Time operator = (const Time& rhs);  // assignment times
    	   const Time operator - () const;
    	   const Time operator + () const;
    	   const Time operator ++ ();
    	   const Time operator ++ (int);
    	   friend ostream& operator << (ostream& ostr, const Time& rhs);
               friend istream& operator >> (istream& istr,  Time& rhs);
    };
    Thanks in advance for any advice/assistance

  2. #2
    Registered User
    Join Date
    Sep 2009
    Posts
    68
    Doh. Solved my != issue rather easily

    Code:
    bool Time::operator != (const Time& rhs) const
    {
        return !(*this == rhs);
    }
    Now my only dilemma is the reduce function and const....

  3. #3
    Registered User hk_mp5kpdw's Avatar
    Join Date
    Jan 2002
    Location
    Northern Virginia/Washington DC Metropolitan Area
    Posts
    3,817
    Hour, Minute and Second are int data members of your class but your operator<< function is attempting to call a "Reduce" member function on those int data members as if they were objects with member functions of their own. If you had to call a Reduce function at all in your operator<< code I would expect it to be called once as such:
    Code:
    ostream& operator << (ostream& ostr, const Time& rhs)
    {
        ostr << rhs.Hour << ":" << rhs.Minute << ":" << rhs.Second;
        rhs.Reduce();
        return ostr;
    }
    But of course since it's not a const member function you aren't allowed to do so. Why do you need to alter the object within the operator<< function? Could you not simply output the data in your code and then call the Reduce function after that point (removing the Reduce call from within the operator<< code?
    Code:
    Time foo;
    ...
    cout << foo << endl;
    foo.Reduce();
    Having the state of an object change because of a simple output seems like violating the laws of thermodynamics... or something.
    Last edited by hk_mp5kpdw; 03-25-2010 at 06:22 AM.
    "Owners of dogs will have noticed that, if you provide them with food and water and shelter and affection, they will think you are god. Whereas owners of cats are compelled to realize that, if you provide them with food and water and shelter and affection, they draw the conclusion that they are gods."
    -Christopher Hitchens

  4. #4
    Registered User
    Join Date
    Sep 2009
    Posts
    68
    hk_mp5kpdw,

    Thanks for your reply man. Yeah I got it figured out. Ended up doing what you said after I realized I was reducing the entire class and not the in individual members.

    Code:
    ostream& operator << (ostream& ostr, const Time& rhs) 
    {
       Time Tmp(rhs);
       Tmp.Reduce();
       ostr << Tmp.Hour << ":" << Tmp.Minute << ":" << Tmp.Second;
       return ostr;
    }

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Compiling C in Visual Studio 2005
    By emanresu in forum C Programming
    Replies: 3
    Last Post: 11-16-2009, 04:25 AM
  2. Recursive function
    By WatchTower in forum C Programming
    Replies: 11
    Last Post: 07-15-2009, 07:42 AM
  3. Screwy Linker Error - VC2005
    By Tonto in forum C++ Programming
    Replies: 5
    Last Post: 06-19-2007, 02:39 PM
  4. Replies: 28
    Last Post: 07-16-2006, 11:35 PM
  5. Dikumud
    By maxorator in forum C++ Programming
    Replies: 1
    Last Post: 10-01-2005, 06:39 AM