Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <iostream>
- #include <string>
- using namespace std;
- class Time24;
- class Time12 {
- private:
- int hh, mm, ss;
- bool PoD;
- public:
- Time12(): hh(0), mm(0), ss(0), PoD(false) {}
- Time12(int sec);
- Time12(Time24 X);
- ~Time12() {}
- int getHH() {return hh;}
- int getMM() {return mm;}
- int getSS() {return ss;}
- bool getPoD() {return PoD;}
- void setHH(int h) { hh = h;}
- void setMM(int m) { mm = m;}
- void setSS(int s) { ss = s;}
- void setPoD(bool pod) { PoD = pod;}
- void readTime();
- void showTime();
- operator int();
- operator Time24();
- };
- class Time24 {
- private:
- int hh, mm, ss;
- public:
- Time24(): hh(0), mm(0), ss(0) {}
- Time24(int sec);
- //Time24(Time12 X);
- ~Time24() {}
- int getHH() {return hh;}
- int getMM() {return mm;}
- int getSS() {return ss;}
- void setHH(int h) { hh = h;}
- void setMM(int m) { mm = m;}
- void setSS(int s) { ss = s;}
- void readTime();
- void showTime();
- operator int() {return (hh * 3600 + mm * 60 + ss);}
- //operator Time12();
- };
- int main() {
- int seconds;
- cout << "Enter Second Value: ";
- cin >> seconds;
- Time24 t24(seconds);
- t24.showTime();
- Time12 t12(seconds);
- t12.showTime();
- t24.readTime();
- seconds = t24;
- cout << "Corresponding Second Value: " << seconds << endl;
- t12.readTime();
- seconds = t12;
- cout << "Corresponding Second Value: " << seconds << endl;
- t24.readTime();
- t12 = t24;
- t12.showTime();
- t12.readTime();
- t24 = t12;
- t24.showTime();
- return 0;
- }
- Time12 :: Time12(Time24 X) {
- ss = X.getSS();
- mm = X.getMM();
- if(X.getHH() > 12) {
- hh = X.getHH() - 12;
- PoD = true;
- }
- else if(X.getHH() == 12) {
- hh = X.getHH();
- PoD = true;
- }
- else if(X.getHH() == 0) {
- hh = 12;
- PoD = false;
- }
- else{
- hh = X.getHH();
- PoD = false;
- }
- }
- Time12 :: Time12(int sec) {
- if(sec > 3600) {
- hh = sec / 3600;
- sec %= 3600;
- }
- if(sec > 60) {
- mm = sec / 60;
- sec %= 60;
- }
- ss = sec;
- PoD = false;
- if(hh > 12) { hh -= 12, PoD = true;}
- else if(hh == 12) { hh = 12, PoD = true;}
- else if(hh == 0) { hh = 12;}
- }
- void Time12 :: readTime() {
- string pod;
- cout << "Enter Time12 Value: ";
- cin >> hh >> mm >> ss >> pod;
- if(pod == "AM" || pod == "am")
- PoD = false;
- else if(pod == "PM" || pod == "pm")
- PoD = true;
- }
- void Time12 :: showTime() {
- cout << "Corresponding Time12 Value: " << hh << " : " << mm << " : " << ss;
- PoD ? cout << " PM" : cout << " AM" << endl;
- }
- Time12 :: operator int() {
- int h;
- if(PoD == true && hh == 12) h = 12;
- else if(PoD == true) h = hh + 12;
- else if(PoD == false && hh == 12) h = 0;
- else h = hh;
- return (h * 3600 + mm * 60 + ss);
- }
- Time12 :: operator Time24() {
- Time24 temp;
- temp.setMM(mm);
- temp.setSS(ss);
- if(PoD == false && hh == 12)
- temp.setHH(0);
- else if(PoD == false && hh < 12)
- temp.setHH(hh);
- else if(PoD == true && hh == 12)
- temp.setHH(12);
- else
- temp.setHH(12 + hh);
- return temp;
- }
- Time24 :: Time24(int sec) {
- hh = sec / 3600;
- sec %= 3600;
- mm = sec / 60;
- sec %= 60;
- ss = sec;
- }
- //Time24 :: Time24(Time12 X) {
- // ss = X.getSS();
- // mm = X.getMM();
- // if(X.getPoD() == true && X.getHH() != 12)
- // hh = X.getHH() + 12;
- // else if(X.getPoD() == false && X.getHH() == 12)
- // hh = 0;
- // else
- // hh = X.getHH();
- //}
- void Time24 :: readTime() {
- cout << "Enter Time24 Value: ";
- cin >> hh >> mm >> ss;
- }
- void Time24 :: showTime() {
- cout << "Corresponding Time24 Value: " << hh << " : " << mm << " : " << ss << endl;
- }
- //Time24 :: operator Time12(){
- // Time12 temp;
- // temp.setMM(mm);
- // temp.setSS(ss);
- // if(hh > 12) {
- // temp.setHH(hh - 12);
- // temp.setPoD(true);
- // }
- // else if(hh == 12) {
- // temp.setHH(12);
- // temp.setPoD(true);
- // }
- // else if(hh < 12 && hh != 0) {
- // temp.setHH(hh);
- // temp.setPoD(false);
- // }
- // else if(hh == 0) {
- // temp.setHH(12);
- // temp.setPoD(false);
- // }
- // return temp;
- //}
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement