Question
This problem is from the C++ Programming Program Design Including Data Structures 7th Edition by D.S. Malik ISBN:978-1-285-85275-1 Exercise 7: This chapter defines the class
This problem is from the C++ Programming Program Design Including Data Structures 7th Edition by D.S. Malik ISBN:978-1-285-85275-1 Exercise 7: This chapter defines the class clockType 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, minutes, or seconds. Also write a program to test your class. Enhance Programming Exercise 7 by adding functions to the class clockType so that a program that uses this class can perform the following operations: a. Returns the elapsed time of the day of a clock in seconds. b. Returns the remaining time of the day of a clock in seconds. c. Determine and output how far apart in time two clocks are? Output the time in the form hr:min:sec Also write a program to test your class.
i got the answer of exercise and here is the answer of 7
clockType.Cpp
//Implementation File for the class clockType #include #include "clockType.h"
using namespace std;
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; }
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; }
void clockType::setHours(int inputHours) { if (inputHours >= 0 && inputHours <= 12)
hr = inputHours; else hr = 0; }
void clockType::setMinutes(int inputMinutes) {
if (inputMinutes >= 0 && inputMinutes <= 60)
min = inputMinutes; else inputMinutes = 0; }
void clockType::setseconds(int inputSeconds) {
if (inputSeconds >= 0 && inputSeconds <= 60)
sec = inputSeconds; else
sec = 0; } void clockType::getHours()
{ cout << hr; }
void clockType::getMinutes()
{ cout << min; }
void clockType::getSeconds() { cout << sec; }
clockType.h
/clockType.h, the specification file for the class clockType
class clockType {
private: int hr, min, sec;
public:
clockType();
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.
void setHours(int inputHours); void setMinutes(int inputMinutes);
void setseconds(int inputSeconds);
void getHours();
void getMinutes();
void getSeconds();
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.
};
Lab13ClockType.cpp
#include < iostream> #include "clockType.h"
using namespace std;
int main()
{
clockType myClock;
myClock.setHours(2); myClock.setMinutes(15); myClock.setseconds(58); cout << "The hours set are: "; myClock.getHours(); cout << endl; cout << "The minutes set are: "; myClock.getMinutes(); cout << endl; cout << "The seconds set are: "; myClock.getSeconds(); cout << endl;
system(" pause ");
return 0; }
Step by Step Solution
There are 3 Steps involved in it
Step: 1
Get Instant Access to Expert-Tailored Solutions
See step-by-step solutions with expert insights and AI powered tools for academic success
Step: 2
Step: 3
Ace Your Homework with AI
Get the answers you need in no time with our AI-driven, step-by-step assistance
Get Started