fork download
  1. #include<iostream>
  2. #include<string>
  3. #include<iomanip>
  4. #include<stdexcept>
  5. using namespace std;
  6. class Time
  7. {
  8. private:
  9. int h,m,s;
  10. public:
  11. Time(int hour=0,int minute=0,int second=0);
  12. void settime(const int hour,const int minute,const int second);
  13. void displaytime24();
  14. void displaytime12();
  15. };
  16. Time::Time(int hour,int minute,int second)
  17. :h(hour),m(minute),s(second){}
  18.  
  19. void Time::settime(const int hour,const int minute,const int second)
  20. {
  21.  
  22. h=hour;
  23. m=minute;
  24. s=second;
  25. if(h<0 || h>23)
  26. {
  27.  
  28. throw runtime_error("hourout");
  29. }
  30.  
  31. }
  32. void Time::displaytime24()
  33. {
  34. cout<<h<<":"<<m<<":"<<s<<endl;
  35. }
  36. void Time::displaytime12()
  37. {
  38. int g,t;
  39. if(h>12 && h<24)
  40. {
  41. t=1;
  42. }
  43. else
  44. {
  45. t=0;
  46. }
  47.  
  48. if((1) || h==24 )
  49. {
  50. g=h-12;
  51. cout<<g<<":"<<m<<":"<<s;
  52. }
  53. else
  54. cout<<h<<":"<<m<<":"<<s;
  55.  
  56. if( (1) || h==12)
  57. cout<<" PM"<<endl;
  58. else
  59. cout<<" AM"<<endl;
  60. }
  61. int main()
  62. {
  63. int h;
  64. try {
  65. cout<<"hour: ";
  66. cin>>h;
  67. }
  68. catch(exception &e)
  69. {
  70. cout<<"Exceptions: "<<e.what()<<endl;
  71. }
  72. Time T(21,22,11);
  73. T.displaytime24();
  74. T.displaytime12();
  75. }
  76.  
Success #stdin #stdout 0s 3468KB
stdin
Standard input is empty
stdout
hour: 21:22:11
9:22:11 PM