class

Hi, Can u please help to solve this programming question.


Q. Design and implement a class "Longtime" using C + + . The class "Longtime" represents the time as hours, minutes and seconds using 24 hour - time format. The class should have a constructor that initialises time to 12:00:00. The class has two member functions as follows :-
(a) "Difference Time" which overloads the - (minus) operator to find the difference of time stored in two different Longtime objects ; and
(b) "Print Time" that prints the time as a.m. or p.m. for example, if a Longtime object has the time stored as 15:15:30, then this print time function will output : 03:15:30 p.m.
Hi there,

Let's break it down:

saiz wrote:
Design and implement a class "Longtime". The class "Longtime" represents the time as hours, minutes and seconds using 24 hour - time format


So you will need to create a class, which has 3 data members: hours, minutes and seconds.

saiz wrote:
The class should have a constructor that initialises time to 12:00:00.


Your constructor will set hours=12, minutes=0 and seconds=0.

saiz wrote:
The class has two member functions as follows :-
(a) "Difference Time" which overloads the - (minus) operator to find the difference of time stored in two different Longtime objects


You will need to overload operator-(). In pseudocode:

longtime longtime::operator-(const longtime& right)
    longtime result;
    result.hours = hours - right.hours
    etc.
    return result


You will need to take into account that hours are 60 minutes and minutes are 60 seconds, you cannot just subtract like I did in the pseudocode.

saiz wrote:
(b) "Print Time" that prints the time as a.m. or p.m. for example, if a Longtime object has the time stored as 15:15:30, then this print time function will output : 03:15:30 p.m.


This is quite straightforward, by determining whether the hour is bigger than 12 (noon) you can determine AM or PM.


If you need any further help, please come back to us with some code you have tried and we'll take it from there.

All the best,
NwN
Topic archived. No new replies allowed.