| | |
Help with creating a class
Please support our C++ advertiser: Intel Parallel Studio Home
Thread Solved |
•
•
Join Date: Nov 2007
Posts: 3
Reputation:
Solved Threads: 0
Hi there^_^,
How can I create a class from this program?
I made many attempts to do so but they all did not work. Here is the latest:
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.^_^
How can I create a class from this program?
C++ Syntax (Toggle Plain Text)
#include <iostream> #include <ctime> using namespace std; int main() { // Obtain the total seconds since the midnight, Jan 1, 1970 int totalSeconds = time(0); // Compute the current second in the minute in the hour int currentSecond = totalSeconds % 60; // Obtain the total minutes int totalMinutes = totalSeconds / 60; // Compute the current minute in the hour int currentMinute = totalMinutes % 60; // Obtain the total hours long totalHours = totalMinutes / 60; // Compute the current hour int currentHour = (int)(totalHours % 24); // Display results cout << "Current time is " << currentHour << ":" << currentMinute << ":" << currentSecond << " GMT" << endl; return 0; }
I made many attempts to do so but they all did not work. Here is the latest:
C++ Syntax (Toggle Plain Text)
#include <iostream> #include <ctime> using namespace std; class Time { private: int hour; int minute; int second; public: Time() { second = time(0); } Time(int newSecond) { setTime(newSecond); } int getHour() { return hour; } int getMinute() { return minute; } int getSecond() { return second; } void setTime(int newSecond) { second = newSecond % 60; int totalMinutes = newSecond / 60; minute = totalMinutes % 60; int totalHours = totalMinutes / 60; hour = totalHours % 24; } }; int main() { Time time1; Time time2(123456); cout << time1.getHour() << ":" << time1.getMinute() << ":" << time1.getSecond() << endl; cout << time2.getHour() << ":" << time2.getMinute() << ":" << time2.getSecond() << endl; return 0; }
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.^_^
•
•
Join Date: Nov 2007
Posts: 3
Reputation:
Solved Threads: 0
YAAAY!^_^ I think I got it!
For all who would like to know my solution:
bye for now!^_^
For all who would like to know my solution:
C++ Syntax (Toggle Plain Text)
#include <iostream> #include <ctime> using namespace std; class Time { private: int hour; int minute; int second; public: Time() { int newSecond; newSecond = time(0); setTime(newSecond); } Time(int newSecond) { setTime(newSecond); } int getHour() { return hour; } int getMinute() { return minute; } int getSecond() { return second; } void setTime(int newSecond) { second = newSecond % 60; int totalMinutes = newSecond / 60; minute = totalMinutes % 60; int totalHours = totalMinutes / 60; hour = totalHours % 24; } }; int main() { Time time1; Time time2(123456); cout << time1.getHour() << ":" << time1.getMinute() << ":" << time1.getSecond() << endl; cout << time2.getHour() << ":" << time2.getMinute() << ":" << time2.getSecond() << endl; return 0; }
bye for now!^_^
![]() |
Similar Threads
- Help on Creating Class in VB6 (Visual Basic 4 / 5 / 6)
- Class in a class... (Java)
- Dynamic class members? (C++)
- Reading an input file as a class memeber function (C++)
- creating a class and declaring objects (C++)
- need help in creating class string (C++)
Other Threads in the C++ Forum
- Previous Thread: Editing Windows Registry
- Next Thread: Delete function for linked list
Views: 2479 | Replies: 1
| Thread Tools | Search this Thread |
Tag cloud for C++
api application array arrays assignment beginner binary bitmap c++ c/c++ calculator char char* class classes code coding compile compiler console conversion convert count data database delete developer display dll email encryption error file forms fstream function functions game generator getline givemetehcodez graph homeworkhelper iamthwee ifstream image input int java lazy lib loop looping loops map math matrix memory multidimensional multiple newbie news node number output parameter pointer problem program programming project proxy python random read recursion recursive reference return sort sorting string strings struct template templates text tree url variable vector video visual visualstudio win32 windows winsock word wordfrequency wxwidgets





