Visual Studio : Create header file with a class called Time that includes three
ID: 3589914 • Letter: V
Question
Visual Studio :
Create header file with a class called Time that includes three pieces of information as data members – an hour (type int), a minute (type int), and a year (type int). Your class should have a constructor with three parameters that uses the parameters to initialize the three data members. For the purpose of this project: • Ensure that the hour value is in the range 0 – 23; if it isn’t, set the hour to 12. • Ensure that the minute value is in the range 0 – 59; if it isn’t, set the minute to 0. • Ensure that the second value is in the range 0 – 59; if it isn’t, set the second to 0. Provide a set and get function for each data member. Provide a member function displayTime that displays the hour, minute and second separated by colons (:). displayTime Function should display hour, minute and second as two digits each. For example, if the value is 11, it should be displayed as 11, and if the value is 1, it should be displayed as 01. Write a test program that demonstrates class Time’s capabilities.
Explanation / Answer
PLEASE REFER BELOW CODE
time_class.h
#include<iostream>
using namespace std;
class Time
{
private:
int hour,minute,second;
public:
Time(int hr,int minte,int sec)
{
hour = hr;
minute = minte;
second = sec;
}
void set_hr(int hr)
{
if(hr < 0 || hr > 23)
hour = 12;
else
hour = hr;
}
int get_hr()
{
return hour;
}
void set_min(int minte)
{
if(minte < 0 || minte > 59)
minute = 0;
else
minute = minte;
}
int get_min()
{
return minute;
}
void set_sec(int sec)
{
if(sec < 0 || sec > 59)
second = 0;
else
second = sec;
}
void displayTime()
{
if(hour <= 9)
cout<<"0"<<hour<<":";
else
cout<<hour<<":";
if(minute <= 9)
cout<<"0"<<minute<<":";
else
cout<<minute<<":";
if(second <= 9)
cout<<"0"<<second<<endl;
else
cout<<second<<endl;
}
};
2) time_class.cpp
#include "time_class.h"
int main()
{
Time T(0,0,0);
T.set_hr(21);
T.set_min(9);
T.set_sec(9);
T.displayTime();
Time T1(5,67,61);
T1.set_hr(5);
T1.set_min(67);
T1.set_sec(61);
T1.displayTime();
return 0;
}
PLEASE REFER BELOW OUTPUT
21:09:09
05:00:00
Process returned 0 (0x0) execution time : 0.020 s
Press any key to continue.
Related Questions
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.