Clock Class HELP

This is a discussion on Clock Class HELP within the C++ Programming forums, part of the General Programming Boards category; hi i have a problem with another class problem. i have this code so far but i am getting a ...

  1. #1
    Is Trying to Learn
    Join Date
    Mar 2006
    Location
    Hutton, Preston
    Posts
    215

    Clock Class HELP

    hi i have a problem with another class problem.

    i have this code so far but i am getting a few problems

    1] it comes up with 2 times and i dont want that
    2] i would like it so output seconds
    3]T3 = T1.Add(T2); T2 is added to T1 and the result returned is the sum of the two times which is assigned to T3.
    4]Will initialise the object T1 to 15:20:35.

    at the moment i have not managed to get it into a function or add the 2 times to to output T3.

    can anyone help?

    here is my code
    Code:
    #include <iostream>
    using namespace std;
    
    class Time 
    {
          public:
                 Time();
                 Time(int hours2, int minutes2);
                 int getHours() const;
                 int getMinutes() const;
                 void setHours(const int hours2);
                 void setMinutes(const int minutes2);
                 Time operator+(const Time &t1) const;
                 void show() const;
                 
          private:       
                 int hours, minutes;                       
                 
    };
    
    Time::Time()
    {
          hours = minutes=0;            
    }
    
    Time::Time(int hours2, int minutes2)
    {
             hours = hours2;
             minutes = minutes2; 
    }
    int Time::getHours() const
    {
            return hours;
    }
    int Time::getMinutes() const
    {
            return minutes;
    } 
    void Time::setHours(const int hours2) 
    {
            hours = hours2;
    }
    void Time::setMinutes(const int minutes2) 
    {
            minutes = minutes2;
    }         
    Time Time::operator+(const Time &t1) const
    {
            Time sum;
            
            sum.minutes = minutes + t1.minutes;
            sum.hours = hours + t1.hours + sum.minutes/60;
            sum.minutes %=60;
            
            return sum;
            
    }
    void Time::show() const
    {
            cout<<hours<<":"<<minutes<<endl;   
    }
    
    
    int main()
    {
    
        Time t1;
        Time t2;
        Time t3;
        
        t1.setHours(2);
        t2.setMinutes(25);    
        t3.show();
        
        t3 = t1 + t2;
        
        t3.show();    
        system("pause");
    }

  2. #2
    Frequently Quite Prolix dwks's Avatar
    Join Date
    Apr 2005
    Location
    Canada
    Posts
    8,048
    I don't really understand your question . . . .

    Right now I can see a few things:
    • system() is in <cstdlib>. There are better ways to keep the console window open: http://faq.cprogramming.com/cgi-bin/...&id=1043284385
    • You call t3.show() before you set t3 to anything, which will output 0:0. Is that what you wanted?
    • How is Time:perator+() supposed to access private members of another Time class? You should use the getHours() and getMinutes() functions.
    dwk

    Seek and ye shall find. quaere et invenies.

    "Simplicity does not precede complexity, but follows it." -- Alan Perlis
    "Testing can only prove the presence of bugs, not their absence." -- Edsger Dijkstra
    "The only real mistake is the one from which we learn nothing." -- John Powell


    Other boards: DaniWeb, TPS
    Unofficial Wiki FAQ: cpwiki.sf.net

    My website: http://dwks.theprogrammingsite.com/
    Projects: codeform, xuni, atlantis, nort, etc.

  3. #3
    The larch
    Join Date
    May 2006
    Posts
    3,573
    Did you write this code yourself? If so, what's the problem?

    have this code so far but i am getting a few problems

    1] it comes up with 2 times and i dont want that
    Time::show is called twice in main(), do you mean that?
    2] i would like it so output seconds
    Well, add int seconds to the class members and change all the methods...
    3]T3 = T1.Add(T2); T2 is added to T1 and the result returned is the sum of the two times which is assigned to T3.
    I see an overloaded + operator. Time::Add would be very similar.

    4]Will initialise the object T1 to 15:20:35.
    Change the constructor...

    at the moment i have not managed to get it into a function or add the 2 times to to output T3.
    After you assign t3 = t1 + t2, it shows 2:25, as it should???

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Specializing class
    By Elysia in forum C++ Programming
    Replies: 6
    Last Post: 09-28-2008, 04:30 AM
  2. Screwy Linker Error - VC2005
    By Tonto in forum C++ Programming
    Replies: 5
    Last Post: 06-19-2007, 02:39 PM
  3. Creating a database
    By Shamino in forum Game Programming
    Replies: 19
    Last Post: 06-10-2007, 01:09 PM
  4. Need help to build network class
    By weeb0 in forum C++ Programming
    Replies: 0
    Last Post: 02-01-2006, 10:33 AM
  5. Abstract class problem
    By VanJay011379 in forum C++ Programming
    Replies: 9
    Last Post: 07-31-2002, 01:30 PM

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