501216 extended time zone class
N/A
Teslim sırasında ödenir
below is what i have so far. i had part of it working and when i added more it got errors. i;d like it to ask for your time zone first and do it that way instead of how i have it right now where it asks for hour/minutes first. it needs to make sure if it turns into a negative number it returns the right time.
here's what it should do:
1. Create a new project which consists of two classes; the base Time class and a derived extTime class.
2. The base class should have data members that are used to represent the hour and the minute of both types of integers. The derived class has one additional data member that represents the time zone. This data member should have a string data type.
Create a function for each of the operations listed below:
1. Base class:
Set the time.
Return the time.
Print the time.
Increment the hour.
This function will take an integer parameter, which is the hours to be added and then mod 24 in case the new value is greater than 23. If the parameter is negative, there is a chance that the new value for hour will be negative. If so, we want to turn it back into a valid positive value.
Increment the minute
This function will take an integer parameter, which is the minute to be added. We perform mod 60 in case the new value is greater than 59. If the parameter is negative, there is a chance that the new value for min will be negative. If so, we want to turn it back into a valid positive value.
A default constructor and a constructor with parameters.
2. Derived class:
Should have (in addition to the classes above) its own class.
Print, which prints the time zone in addition to the time constructor.
1. Construct the main method so that it can test the member functions of each of the parent and the child class by instantiating objects of these classes and calling the appropriate functions.
This is what I've done so far
#include <iostream>
#include <string>
using namespace std;
class Time
{
public:
string zone;//declare time zone
Time() {} //calling function
int hour, minute; //declare hour & min
void setMinute(); //sets mins
void setHour(); //sets hours
int getMinute(){return minute;};//gets minute and returns it
int getHour(){return hour;};//gets hour and returns it
void addMinute();//adds minutes
void addHour();//adds hours
void printTime();//prints time
};
class ExtTime : public Time
{
public:
ExtTime(int hour, int minute)
{
this->hour = hour;
this->minute = minute;
}
void timeZone(int, int);
void printTime();
private:
string zone;
};
void ExtTime::timeZone(int, int)
{
cout << "Enter time zone (EST, PST, MST, CST): ";
cin >> zone;
if (zone == "PST")
cout << "The time is: " << hour << ":" << minute << endl;
if (zone == "MST")
cout << "The time is: " << (hour-1) << ":" << minute << endl;
if (zone == "CST")
cout << "The time is: " << (hour-2) << ":" << minute << endl;
if (zone == "EST")
cout << "The time is: " << (hour-3) << ":" << minute << endl;
}
void Time::setHour()
{
cout << "Enter the hour: ";//asks for user input
cin >> hour;//gets user input
while (hour < 0 || hour > 23);//while loop to keep hours in correct range of 0-23
{ //hours can not equal 24 because that would start the next hour of 00:00
cout << "I'm sorry, but the hour must be between 0 and 23." << endl;
cout << "Please re-enter the hour." << endl;
cin >> hour;//gets user input
}
}
void Time::setMinute()
{
cout << "Enter the minutes: ";//asks for user input
cin >> minute;//gets user input
while (minute < 0 || minute > 59);//while loop to keep minutes in correct range of 0-59.
{ //minutes can not equal 60 as that would be the start of another hour
cout << "I'm sorry, but the minutes must be between 0 and 59." << endl;
cout << "Please re-enter the minutes." << endl;
cin >> minute;//gets user input
}
}
void Time::addMinute()
{
minute ;//increments minute by one
if(minute == 60)//mintues cant be equal to 60
{hour++ ;//adds one hour to the time if minutes equal 60
minute = 0;//resets minutes to 0
if(hour == 24)//hours cant be more than 24
hour = 0;//rest hours to 0
}
}
void Time::addHour()
{
hour ;//increments hours by one
if(hour == 24)//hours cant be equal than 24
hour = 0;//resets hours to 0
}
void Time::printTime()
{
cout << "The new time would be: ";
if(hour < 10)//adds a 0 to hour to make it more professional looking
cout << "0";
cout << hour << ":";
if(minute < 10)//add a 0 to minute to make it more professional looking
cout << "0";
cout << minute;
}
int main()
{
Time myTime;
[url removed, login to view]();//calls function
[url removed, login to view]();//calls function
int h = [url removed, login to view]();//gets hour
int m = [url removed, login to view]();//gets hour
cout << "You entered the time as " << hour << ":" << minute << endl;
[url removed, login to view](); cout << endl;//prints time
cout << endl;//adds blank line to seperate text
[url removed, login to view]();//calls function to add hour
cout << "When we add 1 hour to the time"<< endl;
[url removed, login to view]();cout << endl;//prints time
cout << endl;//adds blank line to seperate text
[url removed, login to view]();//calls function to add minute
cout << "When we add 1 hour to the time"<< endl;
[url removed, login to view]();//prints time
cout << hour;
cout << minute;
cout << endl;
ExtTime newTime(hour,minute);
[url removed, login to view](hour,minute);
//this will be deleted when turned in as its only to keep the window from closing once the program finishes. VB2010 ends program fast, [url removed, login to view] does not
[url removed, login to view](256, '\n');
cout << "Press ENTER to continue..." << endl;
[url removed, login to view]();
//
return 0;
}
Proje NO: #2247137