Friday, July 31, 2009

Help with c++ program on creating a class?

Hi,


How can I create a c++ class from this program? (follow the link): http://www.cs.armstrong.edu/liang/cpp/ex...





I made an attempt to do so but it is not working (i only attached the class part of the program):


http://i152.photobucket.com/albums/s183/...





I also have certain rules I must follow (follow the link):


http://i152.photobucket.com/albums/s183/...





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





I appreciate your help.^_^


Sorry about all of the links but if I typed everything out, I would have greatly exceeded the allowed characters.

Help with c++ program on creating a class?
There could many mistakes :), but some are easy to see - in your setTime function your are initializing local variables "second", "minute" and "hour" - these names will override the names declared on an otside level (in these case the private variables of your class). You want to set the object variables. Then you forgot to use "newSecond" parameter at all. And finally "cSecond", "cMinute" and "cHour" are initialized but not used. All in all "setTime" does nothing :)


This should give you an idea:


void setTime(int newSecond)


{


second = newSecond % 60; // object variable !!!





int totalMinutes = newSecond / 60;


minute = totalMinutes % 60; // object variable !!!





int totalHours = totalMinutes / 24;


hour = totalHours % 24; // object variable !!!


}





Hope this helps.
Reply:i looked at your code and i think i know what's wrong.





in your settime function, you created an object again. this object will then call THE SETTIME function and so on.





it will loop forever. so i suggest that since setTime takes the seconds from midnight. you don't need to create another object inside the settime function.


No comments:

Post a Comment