Advertisement
SMASIF

Data Conversion [Time 24 to Time 12]

Jun 29th, 2015
233
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 2.58 KB | None | 0 0
  1. #include<iostream>
  2. #include<string>
  3. using namespace std;
  4.  
  5. class Time24{
  6.     private:
  7.         int hour, min, sec;
  8.     public:
  9.         Time24():hour(0),min(0),sec(0){}
  10.         Time24(int a, int b, int c):hour(a),min(b),sec(c){}
  11.         ~Time24(){}
  12.         int getHour(){return hour;}
  13.         int getMin(){return min;}
  14.         int getSec(){return sec;}
  15.         void display(){
  16.             cout << hour << ":" << min << ":" << sec << endl;
  17.         }
  18.         // conversion operator Time24 -> int
  19.         operator int(){
  20.             return (hour*3600+min*60+sec);
  21.         }
  22.         // 1-arg constructor int -> Time24
  23.         Time24(int s){
  24.             hour = s/3600;
  25.             min = s/60 - (hour*60);
  26.             sec = s - (min*60) - (hour*3600);
  27.         }
  28.         operator Time12(){
  29.             int h = hour;
  30.             int m = min;
  31.             int s = sec;
  32.             bool day;
  33.             if(h>12){
  34.                 h = h - 12;
  35.                 day = true;
  36.             }
  37.             else if(h==12){
  38.                 day = true;
  39.             }
  40.             else{
  41.                 day = false;
  42.             }
  43.  
  44.             if(h == 0)
  45.                 h = 12;
  46.             return Time12(h,m,s,day);
  47.  
  48.         }
  49.  
  50. };
  51.  
  52. class Time12{
  53.     private:
  54.         int hour, min, sec;
  55.         bool day; // day = false = am, day = true = pm
  56.     public:
  57.         Time12():hour(0),min(0),sec(0),day(false){}
  58.         Time12(int a, int b, int c, bool d):hour(a),min(b),sec(c),day(d){}
  59.         ~Time12(){}
  60.         int getHour(){return hour;}
  61.         int getMin(){return min;}
  62.         int getSec(){return sec;}
  63.         bool getDay(){return day;}
  64.         void display(){
  65.             string str;
  66.             str = (day==false) ? "am" : "pm";
  67.             cout << hour << ":" << min << ":" << sec << " " << str << endl;
  68.         }
  69.         // 1-arg constructor Time24 -> Time12
  70.         Time12(Time24 t){
  71.             hour = t.getHour();
  72.             min = t.getMin();
  73.             sec = t.getSec();
  74.             if(hour>12){
  75.                 hour = hour - 12;
  76.                 day = true;
  77.             }
  78.             else if(hour==12){
  79.                 day = true;
  80.             }
  81.             else{
  82.                 day = false;
  83.             }
  84.  
  85.             if(hour == 0)
  86.                 hour = 12;
  87.  
  88.         }
  89.  
  90. };
  91.  
  92. int main(){
  93.     Time24 t1(23,0,1);
  94.     int seconds = static_cast<int>(t1); // explicit casting
  95.     cout << "Equivalent Seconds: " << seconds << endl;
  96.     Time24 t2(3670);
  97.     t2.display();
  98.     Time12 t3(t1); // invoke 1-arg constructor in Time12 class
  99.     t3.display();
  100.  
  101.     return 0;
  102. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement