954,498 Members — Technology Publication meets Social Media
Username:
Password:
Lost login information?
Have something to say? Contribute New Article Reply to this Article

C++: Using Inheritance (Base & Derived) on Program w/ 2 Clocks w/ TimeZones

Hey there, I was wondering if anyone had the time to look over my code and give me some tips or tweaks to help me get it running. The program is all about inheritance, where i have the derived class using the base class clockType (with using strings) to add time zones (MST, CST, EST, and PST<--as the default zone to start) to the two clocks in the main function. I am also trying to compare the values of clock1 and clock2 with the times zones to see if they are equal or not. The times for the clock can use the user-input style instead.
Sorry about the 'lightness' of the language used and hope some of that made sense... would love to figure this thing out and also understand how I messed this up.

Thankyou!

#include <cstdlib>
#include <iostream>
#include <string>

class clockType
{
    public:
        void setTime(int, int, int);
        void getTime(int&, int&, int&) const;
        void printTime() const;
        void incrementSeconds();
        void incrementMinutes();
        void incrementHours();
        bool equalTime(const clockType&) const;
        clockType(int, int, int);
        clockType();         
        
    private:
        int hr;
        int min;
        int sec;
};

class zoneClockType: public clockType
{
    public:
        void setTimeZone(int, int, int, string);
        void getTimeZone(int&, int&, int&, string, int&) const;
        bool equalTime(const zoneClockType&) const;
        void printTimeAndZone() const;
        zoneClockType(int=0, int=0, int=0, string, string="PST");
        zoneClockType();
          
    private:
        string timeZone;
        int zoneVal;
};
        
using namespace std;

int main(int argc, char *argv[])
{
    int hours, minutes, seconds;
    string...="PST"
    
    clockType clock1;            
    clockType clock2( 23, 13, 75 );
    
    clock1.setTime( 6, 59, 39 );
       cout << "clock1: ";
       clock1.printTimeAndZone();
       cout << endl;
     
     
     clock2.printTimeAndZone();
     cout << endl;
     
   
     clock1.incrementMinutes();
     clock1.printTimeAndZone();
     cout << endl;
    
     clock1.setTime( 0, 13, 0 );
       
    cout << "clock2: ";
    clock2.printTime();
    cout << endl;
    clock2.setTime(5,45,16);
    
    cout << "After setting, clock2: ";
    clock2.printTime();
    cout << endl;
    
    if (clock1.equalTime(clock2))
        cout << "Both times are equal."
             << endl;
      else
          cout << "The two times are different." << endl;    
       
    system("PAUSE");
    return EXIT_SUCCESS;
}

void clockType::setTime(int hours, int minutes, int seconds)
{
    if (0 <= hours && hours < 24)
        hr = hours;
     else
         hr = 0;
        
    if (0 <= minutes && minutes < 60)
        min = minutes;
     else
         min = 0;
         
    if (0 <= seconds && seconds < 60)
        sec = seconds;
     else
         sec = 0;
}

void clockType::getTime(int& hours, int& minutes, int& seconds) const
{
    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::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;
}

void zoneClockType::getTimeZone()
{
    hours=hr;
    minutes=min;
    seconds=sec;
    
    if(timeZone="EST")
        zoneVal=3
    if(timeZone="CST")
        zoneVal=2
    if(timeZone="MST")
        zoneVal=1
    if(timeZone="PST")
        zoneVal=0
}

void zoneClockType::printTimeAndZone()
{
     clockType.printTime();       
     getTimeZone(); 
}

bool zoneClockType::equalTimeZone(const zoneClockType&) const
{
    int h1, h2, m1, m2, s1, s2, v1, v2;
    string z1, z2;
    .
    .
    getTime(h1, m1, s1, z1, v1);
    otherClock.getTime(h2, m2, s2, z2, v2);
    
 if((m1==m2)&&(s1==s2))
        if((h1-h2)-(v1-v2)==0)
            return true;
         else
            return false;
      else
        return false;        

zoneClockType::zoneClockType()
{
     clockType();
     setTimeZone(false);
}

zoneClockType::zoneClockType(int h, int m, int s)
{
     setTime( h, m, s );
     setTimeZone(true);
}
vTr
Newbie Poster
2 posts since Jun 2011
Reputation Points: 10
Solved Threads: 0
 

You need to add year, month and day to the clockType class because you can not compare times if you don't know what days they are on. Suppose you have two times that have the same numeric numbers for hour, minute and second. Are they the same, or are they at least 24 hours apart? Another example: t1 = 1:00 a.m. and t2 = 11:00 p.m. Is that 2 hours difference or 10 hours difference? Again, you don't know if you don't know the dates.

Ancient Dragon
Retired & Loving It
Team Colleague
30,049 posts since Aug 2005
Reputation Points: 5,662
Solved Threads: 2,343
 
You need to add year, month and day to the clockType class because you can not compare times if you don't know what days they are on. Suppose you have two times that have the same numeric numbers for hour, minute and second. Are they the same, or are they at least 24 hours apart? Another example: t1 = 1:00 a.m. and t2 = 11:00 p.m. Is that 2 hours difference or 10 hours difference? Again, you don't know if you don't know the dates.

The clock is in 24 hour format, 0-24, sorry to not make that known earlier. Therefore I guess it is easier to implement that compare function and everything. Can anyone give me some specific fixes on this one, its all good it not though.

vTr
Newbie Poster
2 posts since Jun 2011
Reputation Points: 10
Solved Threads: 0
 

What exactly are you having a problem with?

firstPerson
Senior Poster
3,923 posts since Dec 2008
Reputation Points: 841
Solved Threads: 608
 

This article has been dead for over three months

Post: Markdown Syntax: Formatting Help
You
View similar articles that have also been tagged: