Welcome to Dream.In.Code
Become a C++ Expert!

Join 137,267 C++ Programmers for FREE! Get instant access to thousands of C++ experts, tutorials, code snippets, and more! There are 1,489 people online right now. Registration is fast and FREE... Join Now!




Clock Class HELP

 
Reply to this topicStart new topic

Clock Class HELP

p33ckt
4 Nov, 2006 - 09:33 AM
Post #1

New D.I.C Head
*

Joined: 4 Nov, 2006
Posts: 3



Thanked: 1 times
My Contributions
Hi i am new to this site and new to classed.

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");
}

User is offlineProfile CardPM
+Quote Post

horace
RE: Clock Class HELP
4 Nov, 2006 - 10:56 AM
Post #2

D.I.C Addict
Group Icon

Joined: 25 Oct, 2006
Posts: 573



Thanked: 4 times
Dream Kudos: 50
My Contributions
your class appears to be working, if I change your main() to initialse t1 to 15:20 and t2 to 2:55 thus
CODE

int main()
{
    Time t1=Time(15,20);
    Time t2;
    Time t3;
    
    t2.setHours(2);
    t2.setMinutes(55);    
    t3.show();
    
    t3 = t1 + t2;
    
    t3.show();    
    system("pause");
}

it runs and gives the correct result
0:0
18:15
Press any key to continue . . .

to get it to process seconds you need to declare a new private instance variable seconds, change the constructor to set it, change add to add it, etc

User is offlineProfile CardPM
+Quote Post

p33ckt
RE: Clock Class HELP
4 Nov, 2006 - 11:42 AM
Post #3

New D.I.C Head
*

Joined: 4 Nov, 2006
Posts: 3



Thanked: 1 times
My Contributions
yeah i know it works and every thing but i dont know how to add it into a function like i said on the first post. plus i dont understand how i am going to get it to show the seconds and to get rid of the first time of 0.00 can you help?
User is offlineProfile CardPM
+Quote Post

gregoryH
RE: Clock Class HELP
5 Nov, 2006 - 03:16 AM
Post #4

D.I.C Regular
Group Icon

Joined: 4 Oct, 2006
Posts: 417


Dream Kudos: 50
My Contributions
QUOTE(p33ckt @ 4 Nov, 2006 - 12:42 PM) *

yeah i know it works and every thing but i dont know how to add it into a function like i said on the first post. plus i dont understand how i am going to get it to show the seconds and to get rid of the first time of 0.00 can you help?

p33

I just have to ask this... how did you manage to assemble that code if you don't know how to write a function?
User is offlineProfile CardPM
+Quote Post

p33ckt
RE: Clock Class HELP
5 Nov, 2006 - 12:08 PM
Post #5

New D.I.C Head
*

Joined: 4 Nov, 2006
Posts: 3



Thanked: 1 times
My Contributions
i do know how to use functions but not very well as i am not very good at calling them when they are needed. but if someone can show me and explain what is actually happening that would be a great help as i can learn and then wont need to ask about functions again.

Thanks
User is offlineProfile CardPM
+Quote Post

Reply to this topicStart new topic
Time is now: 12/4/08 07:32PM

Live C++ Help!

C++ Tutorials

Reference Sheets

C++ Snippets

DIC Chatroom

Bye Bye Ads

Monthly Drawing

Thumb Drive

Top Contributors

Top 10 Kudos This Month