Academic Integrity: tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.

IN C++: This chapter defines the class clockType to implement time in a program.

ID: 3710210 • Letter: I

Question

IN C++: 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. Make the functions that retrieve hours, minutes, and seconds as inline. Also, write a program to test your class.

The header file for the class clockType, as it is defined in this chapter, has been provided for you.

There are 3 different tabs. clockType.h, clockTypeImp.cpp, and main.cpp. Given in clockType.h is:

class clockType

{

public:

void setTime(int, int, int);
void getTime(int&, int&, int&) const;
void printTime() const;
void incrementSeconds();
void incrementMinutes();
void incrementHours();
bool equalTime(const clockType&) const;

private:

int hr;
int min;
int sec;

};

Need to know what to add to each tab if you could please specify with comments what goes where. Thank you!

Explanation / Answer

/////// content of clockType.h -- start //////////////////////////////////////

/*clockType.h - this file contains declaration of class clockType.h */

#include <iostream>
using namespace std;   
  
#ifndef __CLOCK_TYPE__
#define __CLOCK_TYPE__ //This is needed to include file only once
  
class clockType   
{   
public:   
void setTime(int, int, int);   
void getTime(int& hr, int& min, int& sec) const;   
void printTime() const;   
void incrementSeconds();   
void incrementMinutes();   
void incrementHours();   
bool equalTime(const clockType& clock) const;   
  
/* New function to be added */
void setHours(int);   
void setMinutes(int);   
void setSeconds(int);   
  
/* inline function 1st form, function automatically becomes inline when defined inside class
but should follow criteria to be an inline function */
int getHours(void)   
{   
return hr;   
}   
int getMinutes(void)   
{   
return min;   
}   
/* inline function another form, when declared inside class but defined outside class */
int getSeconds(void);   
  
clockType(int hour = 0, int minute = 0, int second = 0)   
{   
hr = hour;   
min = min;   
sec = sec;   
}  


private:   
int hr;   
int min;   
int sec;   
};       
#endif

/////// content of clockType.h -- end //////////////////////////////////////

/////// content of clockTypeImpl.cpp -- start //////////////////////////////////////

/*clockTypeImpl.cpp - this file contains definition of function defined in class clockType inside clockType.h file */

#include "clockType.h"

void clockType::setTime(int hr, int min, int sec)
{
cout << " Dummy Function called " << __FUNCTION__ << endl;
cout << " setting hr=" << hr << " min=" << min << " sec=" << sec << endl;
this->hr = hr;
this->min = min;
this->sec = sec;
}
void clockType::getTime(int& hr, int& min, int& sec) const
{
cout << " Dummy Function called " << __FUNCTION__ << endl;
}
void clockType::printTime() const
{
cout << " Dummy Function called " << __FUNCTION__ << endl;
cout << " hours = " << hr << std::endl;
cout << " minutes = " << min << std::endl;
cout << " seconds = " << sec << std::endl;
}
void clockType::incrementSeconds()
{
cout << " Dummy Function called " << __FUNCTION__ << endl;
}
void clockType::incrementMinutes()
{
cout << " Dummy Function called " << __FUNCTION__ << endl;
}
void clockType::incrementHours()
{
cout << " Dummy Function called " << __FUNCTION__ << endl;
}
bool clockType::equalTime(const clockType& time) const
{
cout << " Dummy Function called " << __FUNCTION__ << endl;
}

void clockType::setHours(int hour)
{
this->hr = hour;
}

void clockType::setMinutes(int minute)
{
this->min = minute;
}

void clockType::setSeconds(int second)
{
this->sec = second;
}

/* inline function another form, when defined outside class */
inline int clockType::getSeconds(void)
{
return this->sec;
}

/////// content of clockTypeImpl.cpp -- end //////////////////////////////////////

/////// content of main.cpp -- start //////////////////////////////////////

/* main.cpp - this file defines main function which creates an object of class clockType and tested newly defined set/get functions */

int main()
{
clockType clock;

cout << " Setting clock Hours to 5 " << std::endl;
clock.setHours(5);
cout << " Setting clock Minutes to 30 " << std::endl;
clock.setMinutes(30);
cout << " Setting clock Seconds to 55 " << std::endl;
clock.setSeconds(55);

cout << " Fetch clock hours = " << clock.getHours() << std::endl;
cout << " Fetch clock minutes = " << clock.getMinutes() << std::endl;
cout << " Fetch clock seconds = " << clock.getSeconds() << std::endl;

cout << " Setting clock time using setTime() to 1:2:3 " << std::endl;
clock.setTime(1,2,3);
cout << " print clock time using printTime() " << std::endl;
clock.printTime();

return 0;
}

/////// content of main.cpp -- end //////////////////////////////////////

//////////////// output //////////////////////////////////

Setting clock Hours to 5
Setting clock Minutes to 30
Setting clock Seconds to 55
Fetch clock hours = 5
Fetch clock minutes = 30
Fetch clock seconds = 55
Setting clock time using setTime() to 1:2:3
Dummy Function called setTime
setting hr=1 min=2 sec=3
print clock time using printTime()
Dummy Function called printTime
hours = 1
minutes = 2
seconds = 3