class Time
{
protected:
int hr, min , sec;
string zone;
public:
Time(int h, int m, int s) {hr = h; min = m; sec = s;}
void setHour();
void setMinute();
void setSeconds();
int getHour(){return hr;};
int getMinute(){return min;};
int getSeconds(){return sec;};
void addHour();
void addMinute();
void addSeconds();
void printTime();
};
class ExtTime : public Time
{
public:
ExtTime(int h, int m, int s):Time(h,m,s){}
void timeZone(int, int, int);
void printTime();
private:
string zone;
};
// main.cpp
#include <iostream>
#include <string>
#include "timeZone.h"
using namespace std;
int main()
{
Time myTime;
myTime.setHour();
myTime.setMinute();
myTime.setSeconds();
int h = myTime.getHour();
int m = myTime.getMinute();
int s = myTime.getSeconds();
cout << "The hour is " << h << " and the minute is " << m << "and the second is " << s << endl;
myTime.printTime();
cout << endl;
myTime.addHour();
void Time::addMinute()
{
min++;
if(min == 60)
{
hr++;
min = 0;
if(hr == 24)
hr = 0;
}
}
void Time::addSeconds()
{
sec++;
if( sec == 60)
{
min++;
min = 0;
if(min == 60)
min = 0;
}
}
void Time::printTime()
{
cout << "In Eastern Standard Time the time is: ";
if(hr > 24)
cout << "0";
cout << hr << ":";
if(min > 59)
cout << "0";
cout << min << ":";
if( sec > 59)
cout << "0";
cout << sec;
}
void ExtTime::timeZone(int, int, int)
{
cout << "Enter time zone (EST, PST, MST, CST): ";
cin >> zone;
if (zone == "PST" || zone == "pst")
cout << "The time is: " << (hr-3) << ":" << getMinute() << getSeconds() << endl;
if (zone == "EST" || zone == "est")
cout << "The time is: " << hr << ":" << getMinute() << getSeconds() << endl;
if (zone == "CST" || zone == "cst")
cout << "The time is: " << (hr-1) << ":" << getMinute() << getSeconds() << endl;
if (zone == "MST" || zone =="mst")
cout << "The time is: " << (hr-2) << ":" << getMinute() << getSeconds() << endl;
}
Its not working it was before I added seconds to the mix and then it stopped working and gave me these errors
exit status 1
main.cpp: In function 'int main()':
main.cpp:11:11: error: no matching function for call to 'Time::Time()'
Time myTime;
^~~~~~
In file included from main.cpp:4:
timeZone.h:16:5: note: candidate: 'Time::Time(int, int, int)'
Time(int h, int m, int s) {hr = h; min = m; sec = s;} //calling function
^~~~
timeZone.h:16:5: note: candidate expects 3 arguments, 0 provided
timeZone.h:6:7: note: candidate: 'Time::Time(const Time&)'
class Time
^~~~
timeZone.h:6:7: note: candidate expects 1 argument, 0 provided
timeZone.h:6:7: note: candidate: 'Time::Time(Time&&)'
timeZone.h:6:7: note: candidate expects 1 argument, 0 provided
The class Time has no default constructor. Only this constructor: Time(int h, int m, int s) {hr = h; min = m; sec = s;}
So the only way to create a Time object is to use that constructor. The one that takes three parameters.
While I'm here, there is a bug in your addSeconds() function that is most easily fixed by making it add to the minutes by calling addMinute instead of fiddling around with the minute value. Don't replicate code. If you have a function that adds a minute, and you want to add a minute, call the function. Don't write out another version of the same code. Likewise for adding an hour.