Hello, thanks for visiting my thread.
The assignment requires that I create a class named Time. It has 3 data members, hour, minute, and second (all are ints). This class should have a constructor with 3 parameters to initialize the 3 data members. There are required bounds for each data member. The hour value should be in the range 0-23, if not set it to 12. The minute value should be in the range 0-59, if not set it to 0. The second value should be in the range 0-59, if not set it to 0. A set and get function is required for each data member. Lastly, a member function named "displayTime" that displays the hour, minute, and second separated by colons. Additionally, if any of the values in hour, minute, and second are between 0-9 then it needs to display as "00", "01", "02", etc.
My current roadblock is that I don't know how to initialize a class with more than one parameter. There isn't an example in the text or online exercises that I can work from. I've tried modifying an example that initializes a single data member, but the compiler doesn't seem to like it.
The second roadblock I foresee is the displyTime member function. I literally have no concept of what this member function is supposed to look like in terms of the syntax.
My work-in-progess code is:
Program code:
Code:
#include <iostream>#include <string>
#include "Time.h"
using namespace std;
int main()
{
Time theTime(5,10,10);
cout << "Current time is: " << theTime.displayTime();
cout << "\n Please enter the Hour";
int theHour;
cin >> theHour;
theTime.setHour(theHour);
cout << "\n Please enter the Minute";
int theSecond;
cin >> theSecond;
theTime.setSecond(theSecond);
cout << "\n Please enter the Second";
int theMinute;
cin >> theMinute;
theTime.setMinute(theMinute);
cout << "Current time is: " << theTime.displayTime();
}
Header code:
Code:
#include <string>
class Time {
public:
Time () : hour{0} : minute{0} : second{0} {
}
void setHour (int theHour) {
if (theHour < 0 or theHour > 23)
hour = 12;
else
hour = theHour;
}
int getHour () {
return hour;
}
void setMinute (int theMinute) {
if (theMinute < 0 or theMinute > 59)
minute = 0;
else
minute = theMinute;
}
int getMinute () {
return minute;
}
void setSecond (int theSecond) {
if (theSecond < 0 or theSecond > 59)
second = 0;
else
second = theSecond;
}
int getSecond () {
return second;
}
private:
int hour;
int minute;
int second;
};