I am trying to write a program which will derive one class from another class by adding a data member to store the time zone. Below is what I have so far. However I am unsure on how to write the function to set the time zone. could someone please help?

class extClockType: public clockType
{
  public:
	int getTimeZone();
	void setTimeZone();
	void printTime() const;
	extClockType();
	extClockType(int hours, int minutes, int seconds, int t);
  private:
	int timeZone;
};

int extClockType::getTimeZone()
{
	return timeZone;
}

void extClockType::setTimeZone()
{

}

void extClockType::printTime() const
{
	clockType::printTime();
	cout << "The timezone is: " << timeZone << endl;
}

extClockType::extClockType() 
{
	timeZone = 0;	
}

extClockType::extClockType(int hours, int minutes, int seconds, int t): clockType(hours, minutes, seconds)
{
	timeZone = t;
}

Recommended Answers

All 8 Replies

Should I make up my own clockType, or do you have that too?

oh sorry here it is

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();

private:
    int hr;  
    int min; 
    int sec; 
};



#include <iostream>
#include "clockType.h"

using namespace std;

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)
{
    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()  
{
    setTime(0, 0, 0);
}

how do you want the time zone to be determined?

just have the function bring in an int and set it to timezone... that is unless the time zone is dependant upon something in clocktype

not sure what you are going after

This is what I am confused about as well. I do not understand what will determine the time zone also what format the assignment is looking for GMT or eastern vs central vs western. I have posted all the information I was given to complete this assignment.

i would just have the settimezone function take in an int then and assign it to the timezone variable

if you are testing this class in a program using menus and ussr input just have them enter 0 for GMT and start from there i guess

i think my derived class is now correct

class extClockType: public clockType
{
  public:
	int getTimeZone();
	void setTimeZone(int t);
	void setTime(int hours, int minutes, int seconds, int t);
	void getTime(int& hours, int& minutes, int& seconds, int& t) const;
	void printTime() const;
	extClockType();
	extClockType(int hours, int minutes, int seconds, int t);
  private:
	int timeZone;
};


#include <iostream>
#include "clockType.h"
#include "extClockType.h"

using namespace std;



int extClockType::getTimeZone()
{
	return timeZone;
}

void extClockType::setTimeZone(int t)
{
	timeZone = t;
}

void extClockType::setTime(int hours, int minutes, int seconds, int t)
{
	clockType::setTime(hours, minutes, seconds);
	timeZone = t;
}

void extClockType::getTime(int& hours, int& minutes, int& seconds, int& t) const
{
	clockType::getTime(hours, minutes, seconds);
	t = timeZone;
}

void extClockType::printTime() const
{
	clockType::printTime();
	cout << "The timezone is: " << timeZone << endl;
	if(timeZone >= 0)
    cout << "+" << timeZone;
}

extClockType::extClockType() 
{
	timeZone = 0;	
}

extClockType::extClockType(int hours, int minutes, int seconds, int t): clockType(hours, minutes, seconds)
{
	if(timeZone > 12 || timeZone < -12)
    timeZone = 0;
  else
    timeZone = t;
}

now i am having a problem setting up the main in relation with the derived class. The errors are
newClockType.setTime does not take 1 argument
newClockType.getTime does not take 1 argument
see code below

#include <iostream>
#include "clockType.h"
#include "extClockType.h"

using namespace std;

int main()
{
    clockType myClock;
    clockType yourClock;
	
	extClockType newClockType;
	extClockType oldClockType;
	
	int hours;
    int minutes;
    int seconds;

    myClock.setTime(5, 4, 30);
    newClockType.setTime(8);
	
    cout << "myClock: ";
    myClock.printTime();
    newClockType.printTime();
    cout << endl;

    cout << "yourClock: ";
    yourClock.printTime();
    oldClockType.printTime();
    cout << endl;
      
    yourClock.setTime(5, 45, 16);
    oldClockType.setTime(7);

	cout << "After setting, yourClock: ";
    yourClock.printTime();
    oldClockType.printTime();
    cout << endl;
   
    if (myClock.equalTime(yourClock))
        cout << "Both times are equal."
             << endl;
    else
        cout << "The two times are not equal."
             << endl;

    cout << "Enter the hours, minutes, and "
         << "seconds: ";
    cin >> hours >> minutes >> seconds;
    cout << endl;

    myClock.setTime(hours, minutes, seconds);
    newClockType.setTime(t);
	
    cout << "New myClock: ";
    myClock.printTime();
    newClockType.printTime();
	cout << endl;
     
    myClock.incrementSeconds();

    cout << "After incrementing myClock by "
         << "one second, myClock: ";
    myClock.printTime();
    cout << endl;
    
    myClock.getTime(hours, minutes, seconds);
    newClockType.getTime(t);	
     
    cout << "hours = " << hours 
         << ", minutes = " << minutes
         << ", seconds = " << seconds << endl;


	return 0;
}

now i am having a problem setting up the main in relation with the derived class. The errors are
newClockType.setTime does not take 1 argument
newClockType.getTime does not take 1 argument
see code below

#include <iostream>
#include "clockType.h"
#include "extClockType.h"

using namespace std;

int main()
{
    clockType myClock;
    clockType yourClock;
	
	extClockType newClockType;
	extClockType oldClockType;
	
	int hours;
    int minutes;
    int seconds;

    myClock.setTime(5, 4, 30);
    newClockType.setTime(8);
	
    cout << "myClock: ";
    myClock.printTime();
    newClockType.printTime();
    cout << endl;

    cout << "yourClock: ";
    yourClock.printTime();
    oldClockType.printTime();
    cout << endl;
      
    yourClock.setTime(5, 45, 16);
    oldClockType.setTime(7);

	cout << "After setting, yourClock: ";
    yourClock.printTime();
    oldClockType.printTime();
    cout << endl;
   
    if (myClock.equalTime(yourClock))
        cout << "Both times are equal."
             << endl;
    else
        cout << "The two times are not equal."
             << endl;

    cout << "Enter the hours, minutes, and "
         << "seconds: ";
    cin >> hours >> minutes >> seconds;
    cout << endl;

    myClock.setTime(hours, minutes, seconds);
    newClockType.setTime(t);
	
    cout << "New myClock: ";
    myClock.printTime();
    newClockType.printTime();
	cout << endl;
     
    myClock.incrementSeconds();

    cout << "After incrementing myClock by "
         << "one second, myClock: ";
    myClock.printTime();
    cout << endl;
    
    myClock.getTime(hours, minutes, seconds);
    newClockType.getTime(t);	
     
    cout << "hours = " << hours 
         << ", minutes = " << minutes
         << ", seconds = " << seconds << endl;


	return 0;
}

Part of the reason I've been less responsive is because multiple-file code is a little more unwieldy. But these error messages seem incredibly intuitive:

newClockType.setTime does not take 1 argument
newClockType.getTime does not take 1 argument

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.