In: Computer Science
Extend the definition of the class clockType by overloading the pre-increment and post-increment operator function as a member of the class clockType.
Write the definition of the function to overload the post-increment operator for the class clockType as defined in the step above.
Main.cpp
//Program that uses the class clockType.
#include <iostream>
#include "newClock.h"
using namespace std;
int main()
{
clockType myClock(5, 6, 23);
clockType yourClock;
cout << "Line 3: myClock = " << myClock <<
endl;
cout << "Line 4: yourClock = " << yourClock
<< endl;
cout << "Line 5: Enter time in the form "
<< "hrs:mins:secs ";
cin >> myClock;
cout << "Line 7: New value of myClock = "
<< myClock << endl;
++myClock;
cout << "Line 9: After increment myClock is "
<< myClock << endl;
yourClock.setTime(13, 35, 38);
cout << "Line 11: Now yourClock = "
<< yourClock << endl;
if (myClock == yourClock)
cout << "Line 13: myClock and yourClock "
<< "are equal" << endl;
else
cout << "Line 15: myClock and yourClock "
<< "are not equal" << endl;
if (myClock <= yourClock)
cout << "Line 17: myClock is less than "
<< "yourClock" << endl;
else
cout << "Line 19: myClock is not less "
<< "than yourClock" << endl;
cout << "Line 20: Testing post increment operator"
<< endl;
yourClock = myClock++;
cout << "Line 22: myClock = " << myClock <<
endl;
cout << "Line 23: yourClock = " << yourClock
<< endl;
return 0;
}
newClock.cpp
//Implementation file newClock.cpp
#include <iostream>
#include "newClock.h"
using namespace std;
//Overload the pre-increment operator.
//Overload the post-increment operator.
//Overload the equality operator.
bool clockType::operator==(const clockType& otherClock)
const
{
return (hr == otherClock.hr && min == otherClock.min
&& sec == otherClock.sec);
}
//Overload the not equal operator.
bool clockType::operator!=(const clockType& otherClock)
const
{
return (hr != otherClock.hr || min != otherClock.min
|| sec != otherClock.sec);
}
//Overload the less than or equal to operator.
bool clockType::operator<=(const clockType& otherClock)
const
{
return ((hr < otherClock.hr) ||
(hr == otherClock.hr && min < otherClock.min) ||
(hr == otherClock.hr && min == otherClock.min
&&
sec <= otherClock.sec));
}
//Overload the less than operator.
bool clockType::operator<(const clockType& otherClock)
const
{
return ((hr < otherClock.hr) ||
(hr == otherClock.hr && min < otherClock.min) ||
(hr == otherClock.hr && min == otherClock.min
&&
sec < otherClock.sec));
}
//Overload the greater than or equal to operator.
bool clockType::operator>=(const clockType& otherClock)
const
{
return ((hr > otherClock.hr) ||
(hr == otherClock.hr && min > otherClock.min) ||
(hr == otherClock.hr && min == otherClock.min
&&
sec >= otherClock.sec));
}
//Overload the greater than or equal to operator.
bool clockType::operator>(const clockType& otherClock)
const
{
return ((hr > otherClock.hr) ||
(hr == otherClock.hr && min > otherClock.min) ||
(hr == otherClock.hr && min == otherClock.min
&&
sec > otherClock.sec));
}
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;
}
//Constructor
clockType::clockType(int hours, int minutes, int seconds)
{
setTime(hours, minutes, seconds);
}
//Overload the stream insertion operator.
ostream& operator<<(ostream& osObject, const
clockType& timeOut)
{
if (timeOut.hr < 10)
osObject << '0';
osObject << timeOut.hr << ':';
if (timeOut.min < 10)
osObject << '0';
osObject << timeOut.min << ':';
if (timeOut.sec < 10)
osObject << '0';
osObject << timeOut.sec;
return osObject; //return the ostream object
}
//overload the stream extraction operator
istream& operator>> (istream& is, clockType&
timeIn)
{
char ch;
is >> timeIn.hr; //Step a
if (timeIn.hr < 0 || timeIn.hr >= 24) //Step a
timeIn.hr = 0;
is.get(ch); //Read and discard :. Step b
is >> timeIn.min; //Step c
if (timeIn.min < 0 || timeIn.min >= 60) //Step c
timeIn.min = 0;
is.get(ch); //Read and discard :. Step d
is >> timeIn.sec; //Step e
if (timeIn.sec < 0 || timeIn.sec >= 60) //Step e
timeIn.sec = 0;
return is; //Step f
}
newClock.h
//Header file newClock.h
#ifndef H_newClock
#define H_newClock
#include <iostream>
using namespace std;
class clockType
{
friend ostream& operator<<(ostream&, const
clockType&);
friend istream& operator>>(istream&,
clockType&);
public:
void setTime(int hours, int minutes, int seconds);
//Function to set the member variables hr, min, and sec.
//Postcondition: hr = hours; min = minutes; sec = seconds
void getTime(int& hours, int& minutes, int& seconds)
const;
//Function to return the time.
//Postcondition: hours = hr; minutes = min; seconds = sec
clockType operator++();
//Overload the pre-increment operator.
//Postcondition: The time is incremented by one second.
clockType operator++(int);
// Overload the post increment operator
// Postcondition: Time is incremented by one
second.
bool operator==(const clockType& otherClock) const;
//Overload the equality operator.
//Postcondition: Returns true if the time of this clock
// is equal to the time of otherClock,
// otherwise it returns false.
bool operator!=(const clockType& otherClock) const;
//Overload the not equal operator.
//Postcondition: Returns true if the time of this clock
// is not equal to the time of otherClock,
// otherwise it returns false.
bool operator<=(const clockType& otherClock) const;
//Overload the less than or equal to operator.
//Postcondition: Returns true if the time of this clock
// is less than or equal to the time of
// otherClock, otherwise it returns false.
bool operator<(const clockType& otherClock) const;
//Overload the less than operator.
//Postcondition: Returns true if the time of this clock
// is less than the time of otherClock,
// otherwise it returns false.
bool operator>=(const clockType& otherClock) const;
//Overload the greater than or equal to operator.
//Postcondition: Returns true if the time of this clock
// is greater than or equal to the time of
// otherClock, otherwise it returns false.
bool operator>(const clockType& otherClock) const;
//Overload the greater than operator.
//Postcondition: Returns true if the time of this clock
// is greater than the time of otherClock,
// otherwise it returns false.
clockType(int hours = 0, int minutes = 0, int seconds =
0);
//Constructor to initialize the object with the values
//specified by the user. If no values are specified,
//the default values are assumed.
//Postcondition: hr = hours; min = minutes;
// sec = seconds;
private:
int hr; //variable to store the hours
int min; //variable to store the minutes
int sec; //variable to store the seconds
};
#endif
//newClock.h
#ifndef H_newClock
#define H_newClock
#include <iostream>
using namespace std;
class clockType
{
friend ostream& operator<<(ostream&, const
clockType&);
friend istream& operator>>(istream&,
clockType&);
public:
void setTime(int hours, int minutes, int seconds);
//Function to set the member variables hr, min, and sec.
//Postcondition: hr = hours; min = minutes; sec = seconds
void getTime(int& hours, int& minutes, int& seconds)
const;
//Function to return the time.
//Postcondition: hours = hr; minutes = min; seconds = sec
clockType operator++();
//Overload the pre-increment operator.
//Postcondition: The time is incremented by one second.
clockType operator++(int);
// Overload the post increment operator
// Postcondition: Time is incremented by one second.
bool operator==(const clockType& otherClock) const;
//Overload the equality operator.
//Postcondition: Returns true if the time of this clock
// is equal to the time of otherClock,
// otherwise it returns false.
bool operator!=(const clockType& otherClock) const;
//Overload the not equal operator.
//Postcondition: Returns true if the time of this clock
// is not equal to the time of otherClock,
// otherwise it returns false.
bool operator<=(const clockType& otherClock) const;
//Overload the less than or equal to operator.
//Postcondition: Returns true if the time of this clock
// is less than or equal to the time of
// otherClock, otherwise it returns false.
bool operator<(const clockType& otherClock) const;
//Overload the less than operator.
//Postcondition: Returns true if the time of this clock
// is less than the time of otherClock,
// otherwise it returns false.
bool operator>=(const clockType& otherClock) const;
//Overload the greater than or equal to operator.
//Postcondition: Returns true if the time of this clock
// is greater than or equal to the time of
// otherClock, otherwise it returns false.
bool operator>(const clockType& otherClock) const;
//Overload the greater than operator.
//Postcondition: Returns true if the time of this clock
// is greater than the time of otherClock,
// otherwise it returns false.
clockType(int hours = 0, int minutes = 0, int seconds =
0);
//Constructor to initialize the object with the values
//specified by the user. If no values are specified,
//the default values are assumed.
//Postcondition: hr = hours; min = minutes;
// sec = seconds;
private:
int hr; //variable to store the hours
int min; //variable to store the minutes
int sec; //variable to store the seconds
};
#endif
//newClock.cpp
#include <iostream>
#include "newClock.h"
using namespace std;
clockType clockType::operator++(){
int s = this->sec + 1;
int m = this->min + s/60;
int h = this->hr + m/60;
s%=60;
m%=60;
h%=24;
this->sec = s;
this->min = m;
this->hr = h;
return *this;
}
//Overload the pre-increment operator.
//Postcondition: The time is incremented by one second.
clockType clockType::operator++(int ){
int s = this->sec + 1;
int m = this->min + s/60;
int h = this->hr + m/60;
s%=60;
m%=60;
h%=24;
const clockType old(h,m,s);
return old;
}
//Overload the equality operator.
bool clockType::operator==(const clockType& otherClock)
const
{
return (hr == otherClock.hr && min == otherClock.min
&& sec == otherClock.sec);
}
//Overload the not equal operator.
bool clockType::operator!=(const clockType& otherClock)
const
{
return (hr != otherClock.hr || min != otherClock.min
|| sec != otherClock.sec);
}
//Overload the less than or equal to operator.
bool clockType::operator<=(const clockType& otherClock)
const
{
return ((hr < otherClock.hr) ||
(hr == otherClock.hr && min < otherClock.min) ||
(hr == otherClock.hr && min == otherClock.min
&&
sec <= otherClock.sec));
}
//Overload the less than operator.
bool clockType::operator<(const clockType& otherClock)
const
{
return ((hr < otherClock.hr) ||
(hr == otherClock.hr && min < otherClock.min) ||
(hr == otherClock.hr && min == otherClock.min
&&
sec < otherClock.sec));
}
//Overload the greater than or equal to operator.
bool clockType::operator>=(const clockType& otherClock)
const
{
return ((hr > otherClock.hr) ||
(hr == otherClock.hr && min > otherClock.min) ||
(hr == otherClock.hr && min == otherClock.min
&&
sec >= otherClock.sec));
}
//Overload the greater than or equal to operator.
bool clockType::operator>(const clockType& otherClock)
const
{
return ((hr > otherClock.hr) ||
(hr == otherClock.hr && min > otherClock.min) ||
(hr == otherClock.hr && min == otherClock.min
&&
sec > otherClock.sec));
}
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;
}
//Constructor
clockType::clockType(int hours, int minutes, int seconds)
{
setTime(hours, minutes, seconds);
}
//Overload the stream insertion operator.
ostream& operator<<(ostream& osObject, const
clockType& timeOut)
{
if (timeOut.hr < 10)
osObject << '0';
osObject << timeOut.hr << ':';
if (timeOut.min < 10)
osObject << '0';
osObject << timeOut.min << ':';
if (timeOut.sec < 10)
osObject << '0';
osObject << timeOut.sec;
return osObject; //return the ostream object
}
//overload the stream extraction operator
istream& operator>> (istream& is, clockType&
timeIn)
{
char ch;
is >> timeIn.hr; //Step a
if (timeIn.hr < 0 || timeIn.hr >= 24) //Step a
timeIn.hr = 0;
is.get(ch); //Read and discard :. Step b
is >> timeIn.min; //Step c
if (timeIn.min < 0 || timeIn.min >= 60) //Step c
timeIn.min = 0;
is.get(ch); //Read and discard :. Step d
is >> timeIn.sec; //Step e
if (timeIn.sec < 0 || timeIn.sec >= 60) //Step e
timeIn.sec = 0;
return is; //Step f
}
/Main.cpp
#include <iostream>
#include "newClock.cpp"
using namespace std;
int main()
{
clockType myClock(5, 6, 23);
clockType yourClock;
cout << "Line 3: myClock = " << myClock <<
endl;
cout << "Line 4: yourClock = " << yourClock
<< endl;
cout << "Line 5: Enter time in the form "
<< "hrs:mins:secs ";
cin >> myClock;
cout << "Line 7: New value of myClock = "
<< myClock << endl;
++myClock;
cout << "Line 9: After increment myClock is "
<< myClock << endl;
yourClock.setTime(13, 35, 38);
cout << "Line 11: Now yourClock = "
<< yourClock << endl;
if (myClock == yourClock)
cout << "Line 13: myClock and yourClock "
<< "are equal" << endl;
else
cout << "Line 15: myClock and yourClock "
<< "are not equal" << endl;
if (myClock <= yourClock)
cout << "Line 17: myClock is less than "
<< "yourClock" << endl;
else
cout << "Line 19: myClock is not less "
<< "than yourClock" << endl;
cout << "Line 20: Testing post increment operator"
<< endl;
yourClock = myClock++;
cout << "Line 22: myClock = " << myClock <<
endl;
cout << "Line 23: yourClock = " << yourClock
<< endl;
return 0;
}
//sample output