clockType.h
By: jazy | Date: Oct 2 2007 19:24 | Format: C++ | Expires: never | Size: 3.67 KB | Hits: 1545
- #include <iostream>
- #include <string>
- #include <cctype>
- using namespace std;
- class clockType
- {
- public:
- void setTime(int hours, int minutes, int seconds);
- void getTime(int& hours, int& minutes, int& seconds);
- void printTime() const;
- void incrementSeconds();
- void incrementMinutes();
- void incrementHours();
- bool equalTime(const clockType& otherClock) const;
- clockType(int hours, int minutes, int seconds);
- clockType();
- int returnHr() {
- return hr;
- }
- int returnMin() {
- return min;
- }
- int returnSec() {
- return sec;
- }
- private:
- int hr;
- int min;
- int sec;
- };
- void clockType::setTime(int hours, int minutes, int s)
- {
- if(0 <= hours && hours < 24)
- hr = hours;
- else {
- cout<<"Error with Hour... setting to 0n";
- hr = 00;
- }
- if(0 <= minutes && minutes < 60)
- min = minutes;
- else
- min = 00;
- if(0 <= s && s < 60) {
- sec = s;
- }
- else
- sec = 00;
- }
- void clockType::getTime(int& hours, int& minutes, int& seconds)
- {
- hours = hr;
- minutes = min;
- seconds = sec;
- }
- void clockType::printTime() const
- {
- if(hr < 10)
- cout<<"0";
- cout<<hr<<":";
- if(min < 10)
- cout<<"0";
- cout<<min<<":";
- if(sec < 10)
- cout<<"0";
- cout<<sec;
- }
- void clockType::incrementHours()
- {
- hr++;
- if(hr > 23)
- hr = 0;
- }
- void clockType::incrementMinutes()
- {
- min++;
- if(min > 59)
- {
- min = 0;
- incrementHours();
- }
- }
- void clockType::incrementSeconds()
- {
- sec++;
- if(sec > 59)
- {
- sec = 0;
- incrementMinutes();
- }
- }
- bool clockType::equalTime(const clockType& otherClock) const
- {
- return(hr == otherClock.hr
- && min == otherClock.min
- && sec == otherClock.sec);
- }
- clockType::clockType(int hours, int minutes, int seconds)
- {
- setTime(hours, minutes, seconds);
- }
- clockType::clockType()
- {
- hr = 0;
- min = 0;
- sec = 0;
- }
- class extClockType: public clockType {
- string timeZone;
- public:
- extClockType();
- void printZone();
- void setZone(char *s);
- friend ostream& operator << (ostream& os, extClockType& s) {
- int h,m,sec;
- s.getTime(h,m,sec);
- return os<<h<<":"<<m<<":"<<sec<<" "<<s.timeZone<<endl;
- }
- friend void operator + (extClockType a, extClockType b) {
- if (a.timeZone != b.timeZone)
- cout<<"Time Zones don't match!!";
- cout<<a.returnHr() + b.returnHr();
- cout<<":"<<a.returnMin() + b.returnMin();
- cout<<":"<<a.returnSec() + a.returnSec()<<endl;
- }
- friend bool operator == (extClockType a, extClockType b) {
- if (a.returnHr() == b.returnHr())
- if (a.returnMin() == b.returnMin() )
- if (a.returnSec() == b.returnSec() )
- if (a.timeZone == b.timeZone)
- return true;
- return false;
- }
- friend void operator ++ (extClockType &a) {
- a.incrementSeconds();
- cout<<a;
- }
- friend void operator ++ (extClockType &a,int) {
- cout<<a;
- a.incrementSeconds();
- }
- friend istream& operator>>(istream& in, extClockType& a) {
- int m,h,s;
- cout<<"Enter Hour: ";
- in >> h;
- cout<<"Enter Minute: ";
- in >> m;
- cout<<"Enter Seconds: ";
- in >> s;
- a.setTime(h,m,s);
- }
- };
- extClockType::extClockType() {
- timeZone = "CT";
- }
- void extClockType::printZone()
- {
- cout<<" "<<timeZone<<endl;
- }
- void extClockType::setZone(char s[2]) {
- // puts(s);
- char *p;
- for (p = s; *p != ' '; p++)
- *p = (char) toupper(*p);
- //puts(s);
- if (s[0] != 'P' || s[0] != 'C' || s[0] != 'M' || s[0] != 'E')
- timeZone = "CT";
- if (s[1] != 'T') {
- timeZone = "CT";
- }
- else {
- timeZone = "";
- timeZone += s[0];
- timeZone += s[1];
- }
- }
Latest pastes
1 days ago
1 days ago
2 days ago
3 days ago
5 days ago