before any one tells me that this its bad to do this or its dangerous..ive already heard it on IRC and didnt get an answer that probably would've took them 10 seconds to answer than have a big b!tching fest for 20 minutes on how dangerous they are.
the book im following converts time24 class to an object of time12. military time to civilian time.
heres the 2 classes:
the text in bold, could i name this anything i want? or does it have to be the class that im converting it to?Code:class time12 { private: bool pm; //true = pm, false = am int hrs; //1 to 12 int mins; //0-59 public: time12(): pm(true),hrs(0),mins(0) //contructor to init everything to 0 { } time12(bool ap, int h,int m): pm(ap),hrs(h),mins(m) //3-arg constructor { } void display() const //display time12 { cout << hrs << ':'; if(mins < 10) //extra zero if minutes is less than 10 cout << '0'; cout << mins << ' '; string am_pm = pm ? "p.m." : "a.m."; //create obj of string- if pm=true then "p.m" else "a.m." cout << am_pm; //display string } }; ////////////////////////////////////////////////////// ///////////class time24/////////////////////////////// class time24 { private: int hours; int minutes; int seconds; public: time24(): hours(0),minutes(0),seconds(0) { } time24(int h,int m,int s): hours(h),minutes(m),seconds(s) { } void display() const { if(hours < 10) cout << '0'; cout << hours << ':'; if(minutes < 10) cout << '0'; cout << minutes << ':'; if(seconds < 10) cout << '0'; cout << seconds; } operator time12() const; //conversion operator }; //-------------------------------------------------- time24::operator time12() const //conversion function { int hrs24 = hours; //assign time24.hours to hrs24 bool pm = hours < 12 ? false : true; //if time24.hours < 12 then its a.m else its pm int roundMins = seconds < 30 ? minutes : minutes+1; //round minutes at 30 seconds if(roundMins == 60) //if minutes rounded off higher { //and 60 was the result roundMins = 0; //set minutes to zero ++hrs24; //and add 1 hour to it if(hrs24 == 12 || hrs24 == 24) //set am/pm accordingly if ithits 12 or 24 pm = (pm == true) ? false : true; //toggle am/pm } int hrs12 = (hrs24 < 13) ? hrs24 : hrs24 - 12; if(hrs12 == 0) //00 is 12 a.m. { hrs12 = 12; pm = false; } return time12(pm,hrs12,roundMins); }
for those that cant resist telling me that this is dangerous to use. please tell me why.
thanks in advance![]()



LinkBack URL
About LinkBacks



