need help using C++ This chapter defines the class clock Type to implement time
ID: 3825798 • Letter: N
Question
need help using C++
This chapter defines the class clock Type to implement time in a program. Add functions to this class so that a program that uses this class can set only the hours, minutes, or seconds and retrieve only the hours, Also write a program to test your class.
Create a new project named Lab13ClockType. Then write a program that behaves as described in the textbook’s Chapter 10, Programming Exercise 7, with the modification discussed below. (See pages 652 to 667 for the original clockType class.) Your program should consist of three files:
clockType.h, a header file that declares your class. To save yourself some typing, build upon the original clockType.h file that you’ll find on the course website’s Unit 13 page. clockType.cpp, an implementation file that defines your class’s member functions. Build upon the original clockType.cpp file that you’ll find on the course website’s Unit 13 page.
Lab13ClockType.cpp, a file that tests your class by creating a clockType object and demonstrating the member functions.
Modification: To make things a little easier, change the word “retrieve” to “print” in the second sentence of the book’s directions for this program.
File clockType.h
File ClockType.cpp
please read the instructions carefully
Explanation / Answer
Here is the code for clockType.h:
//clockType.h, the specification file for the class clockType
class clockType
{
public:
void setTime(int hours, int minutes, int seconds);
//Function to set the time.
//The time is set according to the parameters.
//Postcondition: hr = hours; min = minutes;
// sec = seconds;
// The function checks whether the
// values of hours, minutes, and seconds
// are valid. If a value is invalid, the
// default value 0 is assigned.
int getHours();
/*void getTime(int& hours, int& minutes, int& seconds) const;
//Function to return the time.
//Postcondition: hours = hr; minutes = min;
// seconds = sec;
void printTime() const;
//Function to print the time.
//Postcondition: The time is printed in the form
// hh:mm:ss.
void incrementSeconds();
//Function to increment the time by one second.
//Postcondition: The time is incremented by one second.
// If the before-increment time is
// 23:59:59, the time is reset to 00:00:00.
void incrementMinutes();
//Function to increment the time by one minute.
//Postcondition: The time is incremented by one minute.
// If the before-increment time is
// 23:59:53, the time is reset to 00:00:53.
void incrementHours();
//Function to increment the time by one hour.
//Postcondition: The time is incremented by one hour.
// If the before-increment time is
// 23:45:53, the time is reset to 00:45:53.*/
bool equalTime(const clockType& otherClock) const;
//Function to compare the two times.
//Postcondition: Returns true if this time is equal to
// otherClock; otherwise, returns false.
clockType(int hours, int minutes, int seconds);
//Constructor with parameters
//The time is set according to the parameters.
//Postcondition: hr = hours; min = minutes;
// sec = seconds;
// The constructor checks whether the
// values of hours, minutes, and seconds
// are valid. If a value is invalid, the
// default value 0 is assigned.
clockType();
//Default constructor
//The time is set to 00:00:00.
//Postcondition: hr = 0; min = 0; sec = 0;
private:
int hr; //variable to store the hours
int min; //variable to store the minutes
int sec; //variable to store the seconds
};
Here is the code for clockType.cpp:
//Implementation File for the class clockType
#include <iostream>
#include "clockType.h"
using namespace std;
//Set only the hours, minutes, or seconds
void clockType::setTime(int hours, int minutes, int seconds)
{
if (0 <= hours && hours < 24)
hr = hours;
else
hr = 0;
if (0 <= minutes && minutes < 60)
min = minutes;
else
min = 0;
if (0 <= seconds && seconds < 60)
sec = seconds;
else
sec = 0;
}
//retrieve only the hours
int clockType::getHours() { return hours; }
/*void clockType::getTime(int& hours, int& minutes, int& seconds) const
{
hours = hr;
minutes = min;
seconds = sec;
}
void clockType::incrementHours()
{
hr++;
if(hr > 23)
hr = 0;
}
void clockType::incrementMinutes()
{
min++;
if (min > 59)
{
min = 0;
incrementHours();
}
}
void clockType::incrementSeconds()
{
sec++;
if (sec > 59)
{
sec = 0;
incrementMinutes();
}
}*/
void clockType::printTime() const
{
if (hr < 10)
cout << "0";
cout << hr << ":";
if (min < 10)
cout << "0";
cout << min << ":";
if (sec < 10)
cout << "0";
cout << sec;
}
bool clockType::equalTime(const clockType& otherClock) const
{
return (hr == otherClock.hr
&& min == otherClock.min
&& sec == otherClock.sec);
}
clockType::clockType(int hours, int minutes, int seconds)
{
if (0 <= hours && hours < 24)
hr = hours;
else
hr = 0;
if (0 <= minutes && minutes < 60)
min = minutes;
else
min = 0;
if (0 <= seconds && seconds < 60)
sec = seconds;
else
sec = 0;
}
clockType::clockType() //default constructor
{
hr = 0;
min = 0;
sec = 0;
}
I just commented out the unncessary code. If required, you can completely remove that commented code.
Related Questions
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.