Help with creating a class

Please support our C++ advertiser: Intel Parallel Studio Home
Thread Solved

Join Date: Nov 2007
Posts: 3
Reputation: beauty7 is an unknown quantity at this point 
Solved Threads: 0
beauty7 beauty7 is offline Offline
Newbie Poster

Help with creating a class

 
0
  #1
Nov 25th, 2007
Hi there^_^,

How can I create a class from this program?

  1. #include <iostream>
  2. #include <ctime>
  3. using namespace std;
  4.  
  5. int main()
  6. {
  7. // Obtain the total seconds since the midnight, Jan 1, 1970
  8. int totalSeconds = time(0);
  9.  
  10. // Compute the current second in the minute in the hour
  11. int currentSecond = totalSeconds % 60;
  12.  
  13. // Obtain the total minutes
  14. int totalMinutes = totalSeconds / 60;
  15.  
  16. // Compute the current minute in the hour
  17. int currentMinute = totalMinutes % 60;
  18.  
  19. // Obtain the total hours
  20. long totalHours = totalMinutes / 60;
  21.  
  22. // Compute the current hour
  23. int currentHour = (int)(totalHours % 24);
  24.  
  25. // Display results
  26. cout << "Current time is " << currentHour << ":"
  27. << currentMinute << ":" << currentSecond << " GMT" << endl;
  28.  
  29. return 0;
  30. }

I made many attempts to do so but they all did not work. Here is the latest:

  1. #include <iostream>
  2. #include <ctime>
  3. using namespace std;
  4.  
  5. class Time
  6. {
  7. private:
  8. int hour;
  9. int minute;
  10. int second;
  11.  
  12. public:
  13. Time()
  14. {
  15. second = time(0);
  16. }
  17.  
  18. Time(int newSecond)
  19. {
  20. setTime(newSecond);
  21. }
  22.  
  23. int getHour()
  24. {
  25. return hour;
  26. }
  27.  
  28. int getMinute()
  29. {
  30. return minute;
  31. }
  32.  
  33. int getSecond()
  34. {
  35. return second;
  36. }
  37.  
  38. void setTime(int newSecond)
  39. {
  40. second = newSecond % 60;
  41.  
  42. int totalMinutes = newSecond / 60;
  43. minute = totalMinutes % 60;
  44.  
  45. int totalHours = totalMinutes / 60;
  46. hour = totalHours % 24;
  47. }
  48. };
  49.  
  50. int main()
  51. {
  52. Time time1;
  53. Time time2(123456);
  54.  
  55. cout << time1.getHour() << ":" << time1.getMinute() << ":" << time1.getSecond() << endl;
  56. cout << time2.getHour() << ":" << time2.getMinute() << ":" << time2.getSecond() << endl;
  57.  
  58. return 0;
  59. }

My results are:
5138931:6233464:1195994306
10:17:36

But they should be:
(whatever the current time is)
10:17:36

So, I am getting the second part correct but not the first part.

I also have these special rules (click on the following link):
http://i152.photobucket.com/albums/s.../cinstruct.jpg

NOTE: I am not asking for the straight out answer but for some hints and tips on what to do.

I appreciate your help.^_^
Reply With Quote Quick reply to this message  
Join Date: Nov 2007
Posts: 3
Reputation: beauty7 is an unknown quantity at this point 
Solved Threads: 0
beauty7 beauty7 is offline Offline
Newbie Poster

Re: Help with creating a class

 
0
  #2
Nov 25th, 2007
YAAAY!^_^ I think I got it!

For all who would like to know my solution:

  1. #include <iostream>
  2. #include <ctime>
  3. using namespace std;
  4.  
  5. class Time
  6. {
  7. private:
  8. int hour;
  9. int minute;
  10. int second;
  11.  
  12. public:
  13. Time()
  14. {
  15. int newSecond;
  16. newSecond = time(0);
  17. setTime(newSecond);
  18. }
  19.  
  20. Time(int newSecond)
  21. {
  22. setTime(newSecond);
  23. }
  24.  
  25. int getHour()
  26. {
  27. return hour;
  28. }
  29.  
  30. int getMinute()
  31. {
  32. return minute;
  33. }
  34.  
  35. int getSecond()
  36. {
  37. return second;
  38. }
  39.  
  40. void setTime(int newSecond)
  41. {
  42.  
  43. second = newSecond % 60;
  44.  
  45. int totalMinutes = newSecond / 60;
  46. minute = totalMinutes % 60;
  47.  
  48. int totalHours = totalMinutes / 60;
  49. hour = totalHours % 24;
  50. }
  51. };
  52.  
  53. int main()
  54. {
  55. Time time1;
  56. Time time2(123456);
  57.  
  58. cout << time1.getHour() << ":" << time1.getMinute() << ":" << time1.getSecond() << endl;
  59. cout << time2.getHour() << ":" << time2.getMinute() << ":" << time2.getSecond() << endl;
  60.  
  61. return 0;
  62. }

bye for now!^_^
Reply With Quote Quick reply to this message  
Reply

This thread has been marked solved.
Perhaps start a new thread instead?
Message:




Views: 2479 | Replies: 1
Thread Tools Search this Thread



Tag cloud for C++
About Us | Contact Us | Advertise | DaniWeb | Acceptable Use Policy | RSS Feed

©2003 - 2009 DaniWeb® LLC