Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include<iostream>
- #include<string>
- using namespace std;
- class Time24{
- private:
- int hour, min, sec;
- public:
- Time24():hour(0),min(0),sec(0){}
- Time24(int a, int b, int c):hour(a),min(b),sec(c){}
- ~Time24(){}
- int getHour(){return hour;}
- int getMin(){return min;}
- int getSec(){return sec;}
- void display(){
- cout << hour << ":" << min << ":" << sec << endl;
- }
- // conversion operator Time24 -> int
- operator int(){
- return (hour*3600+min*60+sec);
- }
- // 1-arg constructor int -> Time24
- Time24(int s){
- hour = s/3600;
- min = s/60 - (hour*60);
- sec = s - (min*60) - (hour*3600);
- }
- operator Time12(){
- int h = hour;
- int m = min;
- int s = sec;
- bool day;
- if(h>12){
- h = h - 12;
- day = true;
- }
- else if(h==12){
- day = true;
- }
- else{
- day = false;
- }
- if(h == 0)
- h = 12;
- return Time12(h,m,s,day);
- }
- };
- class Time12{
- private:
- int hour, min, sec;
- bool day; // day = false = am, day = true = pm
- public:
- Time12():hour(0),min(0),sec(0),day(false){}
- Time12(int a, int b, int c, bool d):hour(a),min(b),sec(c),day(d){}
- ~Time12(){}
- int getHour(){return hour;}
- int getMin(){return min;}
- int getSec(){return sec;}
- bool getDay(){return day;}
- void display(){
- string str;
- str = (day==false) ? "am" : "pm";
- cout << hour << ":" << min << ":" << sec << " " << str << endl;
- }
- // 1-arg constructor Time24 -> Time12
- Time12(Time24 t){
- hour = t.getHour();
- min = t.getMin();
- sec = t.getSec();
- if(hour>12){
- hour = hour - 12;
- day = true;
- }
- else if(hour==12){
- day = true;
- }
- else{
- day = false;
- }
- if(hour == 0)
- hour = 12;
- }
- };
- int main(){
- Time24 t1(23,0,1);
- int seconds = static_cast<int>(t1); // explicit casting
- cout << "Equivalent Seconds: " << seconds << endl;
- Time24 t2(3670);
- t2.display();
- Time12 t3(t1); // invoke 1-arg constructor in Time12 class
- t3.display();
- return 0;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement