Conversion functions

This is a discussion on Conversion functions within the C++ Programming forums, part of the General Programming Boards category; I have to Add to the Time class the conversion function for converting from a long (seconds 77777) to a ...

  1. #1
    Registered User
    Join Date
    Oct 2002
    Posts
    22

    Conversion functions

    I have to Add to the Time class the conversion function for converting from a long (seconds 77777) to a Time object....I have no idea what to do...this is the code I have so far....

    Code:
    class Time
       {
       private:
          int hrs;                       //1 to 23
          int mins;                      //0 to 59
          int secs;			     //0 to 59
       public:                           //no-arg constructor
          Time() : hrs(0), mins(0), secs(0)
             {  }
                                         //3-arg constructor
          Time(int h, int m, int s) : hrs(h), mins(m), secs(s)
             {  }
          void display() const           //format: 11:59 p.m.
             {
             cout << hrs << ':' << mins << ':' << secs;
             }
    
             Time operator ++ ()         // increment prefix
             { return Time (++hrs, ++ mins, ++ secs);}
    
             Time operator ++ (int)      // increment postfix
             { return Time (hrs++, mins++, secs++);}
    
             Time operator -- ()         //prefix
             {return Time (--hrs, --mins, --secs);}
    
             Time operator -- (int)      //postfix
             {return Time (hrs--, mins--, secs--);}
    
    };
    ////////////////////////////////////////////////////////////////
    
    void main(void)
       {
       int h, m, s;
       long time;
    
       cout << "Enter 24-hour time: \n";
       cout << "   Hours (0 to 23): "; cin >> h;
       cout << "   Minutes: ";  cin >> m;
       cout << "   Seconds: ";  cin >> s;
    
       Time t(h, m, s);           //make a time object t
       cout << "You entered: ";   //display the t
       t.display();
    
    
      
    }
    
    I think i need a conversion operator ... but not to sure how to go about it....plz help.....thx

  2. #2
    Senior Member joshdick's Avatar
    Join Date
    Nov 2002
    Location
    Phildelphia, PA
    Posts
    1,146

    Re: Conversion functions

    Originally posted by Bones
    Code:
          int hrs;                       //1 to 23
    Hours go from zero to 23, as you were using them in the rest of your class.

    Code:
          void display() const           //format: 11:59 p.m.
             {
             cout << hrs << ':' << mins << ':' << secs;
             }
    Instead of this, have you thought about overloading the << operator? Then, instead of
    Code:
    t.display();
    //you could do this:
    cout << t;
    Okay, and about those increment and decrement operators, your implementation is wrong. Time does not progress by an hour, minute, and second. It progresses by one second at a time. The implementation of your increment operator needs to reflect that basic understanding of time. I'm not sure why you need a decrement operator, unless of course, you've discovered time travel in which case, your talents are far greater in physics than programming

    Code:
    void main(void)
    No. Noooooooooo. Bad coder. Here's what you should do:
    Code:
    int main()
    {
         // statements and such
         return 0;
    }
    Code:
       cout << "Enter 24-hour time: \n";
       cout << "   Hours (0 to 23): "; cin >> h;
       cout << "   Minutes: ";  cin >> m;
       cout << "   Seconds: ";  cin >> s;
    Please put seperate expressions on seperate lines. That code there is difficult to read.

    And, instead of having them enter the hours, minutes, and seconds seperately, you could overload the >> operator so that the user could enter time all at once in the form HH:MM:SS.

    I hope that helps
    Last edited by joshdick; 09-03-2003 at 03:25 PM.
    FAQ

    "The computer programmer is a creator of universes for which he alone is responsible. Universes of virtually unlimited complexity can be created in the form of computer programs." -- Joseph Weizenbaum.

    "If you cannot grok the overall structure of a program while taking a shower, you are not ready to code it." -- Richard Pattis.

  3. #3
    Senior Member joshdick's Avatar
    Join Date
    Nov 2002
    Location
    Phildelphia, PA
    Posts
    1,146
    Whoops, I didn't exactly answer your question. Silly me.

    You can write a constructor that takes a single argument of seconds and then convert that into minutes and seconds and then hours, minutes, and seconds.

    Code:
    Time(int seconds)
    {
         secs = seconds % 60;
         mins = seconds / 60;
         hrs = mins / 60;
         mins = mins % 60;
    }
    
    // Prove to myself that this works:
    time(7000);
    secs = 7000 % 60;
    secs = 40;
    mins = 7000 / 60;
    mins = 101;
    hrs = 1;
    mins = 101 / 60;
    mins = 41;
    
    time(7000) == 1:41:40;
    Okay, that looks like a solution to me.
    FAQ

    "The computer programmer is a creator of universes for which he alone is responsible. Universes of virtually unlimited complexity can be created in the form of computer programs." -- Joseph Weizenbaum.

    "If you cannot grok the overall structure of a program while taking a shower, you are not ready to code it." -- Richard Pattis.

  4. #4
    Just Lurking Dave_Sinkula's Avatar
    Join Date
    Oct 2002
    Posts
    5,006
    Originally posted by joshdick
    // Prove to myself that this works:
    time(7000) == 1:41:40;
    Okay, that looks like a solution to me.
    You might want to find the 15 minutes that are missing.

    [edit]
    7000 seconds != 1:41:40
    7000 seconds == 1:56:40
    [/edit]
    Last edited by Dave_Sinkula; 09-03-2003 at 10:03 PM.
    7. It is easier to write an incorrect program than understand a correct one.
    40. There are two ways to write error-free programs; only the third one works.*

  5. #5
    Registered User
    Join Date
    Oct 2001
    Posts
    2,934
    Originally posted by Dave_Sinkula
    You might want to find the 15 minutes that are missing.
    He just discovered time travel?

  6. #6
    Senior Member joshdick's Avatar
    Join Date
    Nov 2002
    Location
    Phildelphia, PA
    Posts
    1,146
    Originally posted by swoopy
    He just discovered time travel?
    Hmmm, I always wanted to do that.
    FAQ

    "The computer programmer is a creator of universes for which he alone is responsible. Universes of virtually unlimited complexity can be created in the form of computer programs." -- Joseph Weizenbaum.

    "If you cannot grok the overall structure of a program while taking a shower, you are not ready to code it." -- Richard Pattis.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Is it legal to have functions within functions?
    By Programmer_P in forum C++ Programming
    Replies: 13
    Last Post: 05-26-2009, 12:21 AM
  2. An array of macro functions?
    By someprogr in forum C Programming
    Replies: 6
    Last Post: 01-28-2009, 07:05 PM
  3. Conversion of pointers to functions
    By hzmonte in forum C Programming
    Replies: 0
    Last Post: 01-20-2009, 01:56 AM
  4. Inline functions and inheritance
    By hpy_gilmore8 in forum C++ Programming
    Replies: 3
    Last Post: 01-14-2004, 06:46 PM
  5. Passing pointers between functions
    By heygirls_uk in forum C Programming
    Replies: 5
    Last Post: 01-09-2004, 06:58 PM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21