Inheritance c++

hi there, i have completed the code, but having trouble with the zones, if someone can pls point out what am i doing wrong. thanks

#include <iostream>
#include <string>

using namespace std;

class Time
{
protected: //protected do they will be known in 'ExtTime'
int hour, minute; //declare hour & min
char zone; //declare time zone

public:
Time() {} //calling function
Time(int h, int m, char zone); //initialize hr and min to input
void setHour(); //sets hours
void setMinute(); //sets mins
int getHour(){return hour;};
int getMinute(){return minute;};
void addHour();
void addMinute();
void printTime();
};

class ExtTime : public Time
{
public:
ExtTime();
ExtTime(int h, int m, string z);
void timeZone();
void printTime();

private:
string zone;
};

void ExtTime::timeZone()
{
if (zone == "PST")
cout << "The time is: " << (getHour()-3) << ":" << getMinute() << endl;
if (zone == "EST")
cout << "The time is: " << getHour() << ":" << getMinute() << endl;
if (zone == "CST")
cout << "The time is: " << (getHour()-1) << ":" << getMinute() << endl;
if (zone == "MST")
cout << "The time is: " << (getHour()-2) << ":" << getMinute() << endl;
}

Time::Time(int h, int m, char z)
{
hour = h;
minute = m;
}

ExtTime::ExtTime(int h, int m, string z)
{
zone = z;
hour = h;
minute = m;
}

void Time::setHour()
{
cout << "Enter the hour: ";
cin >> hour;
}

void Time::setMinute()
{
cout << "Enter the minutes: ";
cin >> minute;
}



void Time::addHour()
{
hour++;
if(hour == 24)
hour = 0;
}

void Time::addMinute()
{
minute++;
if(minute == 60)
{hour++;
minute = 0;
if(hour == 24)
hour = 0;
}
}

void Time::printTime()
{
cout << "The time is: ";
if(hour < 10)
cout << "0";
cout << hour << ":";
if(minute < 10)
cout << "0";
cout << minute;
}

void ExtTime::printTime()
{
Time::printTime();
cout << " " << zone;
}


int main()
{
ExtTime tz(3, 4, "EST");
tz.printTime();
cout << endl;
Time myTime;
myTime.setHour();
myTime.setMinute();
int h = myTime.getHour();
int m = myTime.getMinute();
cout << "The hour is " << h << " and the minute is " << m << endl;
myTime.printTime();
cout << endl;
myTime.addHour();
myTime.addMinute();
cout << "After adding 1 hour and 1 minute is\n";
myTime.printTime();
cout << "\n\nPress 'Enter' to exit." << flush;
cin.sync(); //flush cin stream
cin.get();
return 0;
}
char zone; //declare time zone

if (zone == "PST")

This doesn't work because you're comparing a char with a const char*. Try making zone a string.
i tried that, still got the same results
What exactly is the problem?
shacktar, i am not getting the time zone at all....maybe i am not explaining right..
When I run the program, it prints at the beginning:

The time is: 03:04 EST

so ExtTime tz has its zone member populated correctly.

Is it because you're expecting this line myTime.printTime(); to print a time zone?
If so, that's a Time object and its printTime(); method does not print out zone. With your code as is, the myTime instance should be changed to a ExtTime instance if you want the time zone printed.

Another thing is Time has a zone member declared as a char and ExtTime has a zone member declared as a string. The zone member in ExtTime hides the zone member in Time. Why not make zone a string in the base class and remove it from the derived class?
Last edited on
Topic archived. No new replies allowed.