944,113 Members | Top Members by Rank

Ad:
  • C++ Discussion Thread
  • Marked Solved
  • Views: 4842
  • C++ RSS
Nov 25th, 2007
0

Help with creating a class

Expand Post »
Hi there^_^,

How can I create a class from this program?

C++ Syntax (Toggle Plain Text)
  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:

C++ Syntax (Toggle Plain Text)
  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.^_^
Similar Threads
Reputation Points: 10
Solved Threads: 0
Newbie Poster
beauty7 is offline Offline
3 posts
since Nov 2007
Nov 25th, 2007
0

Re: Help with creating a class

YAAAY!^_^ I think I got it!

For all who would like to know my solution:

C++ Syntax (Toggle Plain Text)
  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!^_^
Reputation Points: 10
Solved Threads: 0
Newbie Poster
beauty7 is offline Offline
3 posts
since Nov 2007

This thread is solved

Either the thread starter or a moderator has marked this thread as solved. You can most likely trust the responses and answers given. There is most likely no reason for any further responses to be posted here. If you have a related question, please start a new thread in this forum instead.

This thread is more than three months old

No one has posted to this discussion for at least three months. Please let old threads die and do not reply to them unless you feel you have something new and valuable to contribute that absolutely must be added to make the discussion complete. Otherwise, please start a new thread in this forum instead.
Message:
Previous Thread in C++ Forum Timeline: Editing Windows Registry
Next Thread in C++ Forum Timeline: Delete function for linked list





About Us | Contact Us | Advertise | Acceptable Use Policy
Forum Index | Build Custom RSS Feed


Follow us on Twitter


© 2011 DaniWeb® LLC