In: Computer Science
Complete the definitions for the following prototypes in the Time class:
Time(std::string) – a constructor that will take in a string with the format h:m:s and parse the
h, m and s into separate int values that can be used to set the time of a new time object.
int getSecond() const - return the value of the second void setSecond() - set the value of the second, making sure the new second value is valid before changing it.
std::string to24format() - return a string with the time in the format hh:mm:ss
Starting point for lab 3: https://repl.it/@jholst/Time-class-lab-3-starting-point
Code
Time.h
#include <string>
#ifndef TIME_H
#define TIME_H
class Time{
public:
// constructors
Time (int = 0, int = 0, int = 0);
Time (std::string); // lab 3 define
// set functions
void setTime(int, int, int);
// get functions
int getHour() const;
int getMinute() const;
int getSecond() const; // lab 3 define
// display functions
std::string to24format() const; // lab 3 define
std::string toAMPMformat() const;
private:
// data members
int hour;
int minute;
int second;
// private helper functions
void setHour(int);
void setMinute(int);
void setSecond(int); // lab 3 define
};
#endif
Time.cpp
#include <string>
#include <sstream>
#include <iostream>
#include <iomanip>
#include "Time.h"
// constructor to take three int values
Time::Time(int hour, int minute, int second){
setTime(hour, minute, second);
}
Time::Time(std::string time)
{
std::string temp;
size_t found = time.find(":");
temp=time.substr(0,found);
setHour(stoi(temp));
time=time.substr(found+1);
found = time.find(":");
temp=time.substr(0,found);
setMinute(stoi(temp));
time=time.substr(found+1);
setSecond(stoi(time));
}
void Time::setTime(int h, int m, int s){
setHour(h);
setMinute(m);
setSecond(s);
}
void Time::setHour(int h){
if (h >= 0 && h < 24){
hour = h;
}
else{
std::cout << "\nInvalid hour value " << h <<
"\n";
}
}
void Time::setMinute(int m){
if (m >= 0 && m < 60){
minute = m;
}
else{
std::cout << "\nInvalid minute value " << m <<
"\n";
}
}
void Time::setSecond(int s)
{
if (s >= 0 && s < 60){
second = s;
}
else{
std::cout << "\nInvalid minute value " << s <<
"\n";
}
}
int Time::getHour() const {
return hour;
}
int Time::getMinute() const {
return minute;
}
int Time::getSecond() const
{
return second;
}
std::string Time::to24format() const
{
std::ostringstream output;
output << std::setfill('0') << std::setw(2)
<< ((hour == 0 || hour == 12) ? 12 : hour)
<< ":" << std::setw(2) << minute
<< ":" << std::setw(2) << second ;
return output.str();
}
std::string Time::toAMPMformat() const{
std::ostringstream output;
output << std::setfill('0') << std::setw(2)
<< ((hour == 0 || hour == 12) ? 12 : hour % 12)
<< ":" << std::setw(2) << minute
<< ":" << std::setw(2) << second
<< (hour < 12 ? " AM" : " PM");
return output.str();
}
Main.cpp
#include <iostream>
#include <sstream>
#include "Time.h"
using namespace std;
void displayTime(const string & message, const Time *
time){
cout << message << "\n24 Hour clock: " <<
time->to24format() << "\nAM/PM format: " <<
time->toAMPMformat() << "\n\n";
}
int main() {
Time time1;
Time time2(11);
Time time3(22, 30);
Time time4(7, 45, 30);
displayTime("Time 1", &time1);
displayTime("Time 2", &time2);
displayTime("Time 3", &time3);
displayTime("Time 4", &time4);
string rawinput;
cout << "Enter time h:m:s = ";
getline(cin, rawinput);
istringstream inputTime(rawinput);
int hour, minute, second;
char c;
inputTime >> hour >> c >> minute >> c
>> second;
Time newTime(hour, minute, second);
displayTime("New time", &newTime);
// lab 3 addition of constructor taking string
Time newStringTime("5:30:15"); // use new constructor
displayTime("New string time", &newStringTime);
}
output
If you have any query regarding the code please ask me in the
comment i am here for help you. Please do not direct thumbs down
just ask if you have any query. And if you like my work then please
appreciates with up vote. Thank You.