In: Computer Science
Question 5
The following is a description of the Time class and its actual Time.h:
(A.) Four Member Variables
(1.) hour (int)
(2.) minute (int)
(3.) second (int)
(4.) count (static int)
(B.) Constructors (Each constructor should increment the count static member variable)
(1.) A Default Constructor that initializes the time to 0:00:00
(2.) A Single Parameter Constructor that initializes the time to x:00:00 (where x is the parameter value)
(3.) A Double Parameter Constructor that initializes the time to x:y:00 (where x and y are the 1st and 2nd parameter values respectively)
(4.) A Triple Parameter Constructor that initializes the time to x:y:z (where x, y, and z are the 1st, 2nd, and 3rd parameter values respectively)
(C.) Accessor Functions for the 3 non-static member variables
(D.) Mutator Functions for the 3 non-static member variables
(E.) A Static Accessor Function for the static member variable
(F.) An Input Function that uses console input to change the values of the Time instance
(G.) An Output Function that displays the Time object on the console in the format - hour:minute:second
/****************** Time.h *********************/
#ifndef TIME_H
#define TIME_H
#include <iostream>
#include <iomanip>
class Time {
public:
Time();
Time(int h);
Time(int h, int m);
Time(int h, int m, int s);
//Accessor
int getHour() const;
int getMinute() const;
int getSecond() const;
static int getCount();
//Mutator
void setHour(int h);
void setMinute(int m);
void setSecond(int s);
void input();
void output() const;
private:
int hour, minute, second;
static int count;
};
#endif
Here is the main.cpp:
/****************** main.cpp *********************/
#include <iostream>
#include <iomanip>
#include "Time.h"
int main() {
Time t1, t2(3), t3(3,5), t4(15, 25, 35), t5(7);
std::cout << "First time object:\n";
t1.output();
t1.input();
t1.output();
std::cout << std:: endl;
std::cout << "Second time object:\n";
t2.output();
t2.input();
t2.output();
std::cout << std:: endl;
t3.output();
t4.output();
t5.output();
std::cout << "The total count is " << Time::getCount()
<< std::endl;
return 0;
}
And a sample run:
[ctse@venus fa20]$ g++ Time.cpp main.cpp
[ctse@venus fa20]$ ./a.out
First time object:
Time instance = 00:00:00
Change hour? Y/N
Y
Enter hour: 1
Change minute? Y/N
N
Change second? Y/N
N
Time instance = 01:00:00
Second time object:
Time instance = 03:00:00
Change hour? Y/N
N
Change minute? Y/N
N
Change second? Y/N
N
Time instance = 03:00:00
Time instance = 03:05:00
Time instance = 15:25:35
Time instance = 07:00:00
The total count is 5
[ctse@venus fa20]$
Provide the Time.cpp (You don’t need to comment).
//IF YOU ARE SATISFIED WITH THE CODE, KINDLY LEAVE A LIKE, OR ELSE COMMENT TO CLEAR DOUBTS
//AS ASKED ONLY PROVIDED THE TIME.CPP
Time.cpp
#include <iostream>
#include <iomanip>
#include "Time.h"
using namespace std;
int Time::count = 0;
Time::Time(){
hour = minute = second = 0;
++count;
}
Time::Time(int h){
hour = h;
minute = second = 0;
++count;
}
Time::Time(int h, int m){
hour = h;
minute = m;
second = 0;
++count;
}
Time::Time(int h, int m, int s)
{
hour = h;
minute = m;
second = s;
++count;
}
int Time::getHour() const{
return hour;
}
int Time::getMinute() const
{
return minute;
}
int Time::getSecond() const
{
return second;
}
void Time::setHour(int h){
hour = h;
}
void Time::setMinute(int m)
{
minute = m;
}
void Time::setSecond(int s)
{
second = s;
}
int Time::getCount(){
return count;
}
void Time::output() const{
cout<<"\nTime instance = ";
cout << setw(2) << setfill('0') << hour << ":";
cout << setw(2) << setfill('0') << minute << ":";
cout << setw(2) << setfill('0') << second;
cout<<endl;
}
void Time::input(){
char choice;
int temp;
cout<<"\nChange hour? (Y/N) :";
cin>>choice;
if(choice == 'Y' || choice == 'y'){
cout<<"Enter hour: ";
cin>>temp;
setHour(temp);
}
cout << "Change minute? (Y/N) :";
cin >> choice;
if (choice == 'Y' || choice == 'y')
{
cout << "Enter minute: ";
cin >> temp;
setMinute(temp);
}
cout << "Change second? (Y/N) :";
cin >> choice;
if (choice == 'Y' || choice == 'y')
{
cout << "Enter second: ";
cin >> temp;
setSecond(temp);
}
}
CODE OUTPUT: