I need this programmed in C++ and it must be split into .CPP and .H files. Desig
ID: 3728763 • Letter: I
Question
I need this programmed in C++ and it must be split into .CPP and .H files.
Design a class called Clock. This class needs three member variables hours, minutes, and seconds. The class should have a default constructor and a constructor that accepts hours, minutes, and seconds. It should also have member functions for storing and retrieving the hours, minutes and seconds. The class should also have the following overloaded operators: Addition operator. When two clock objects are added together, the overloaded + operator should return the sum of the two object's hours, minutes, and seconds, converted into valid hours, minutes and seconds + Subtraction operator. When one clock object is subtracted from another clock object, the overloaded operator should return the difference of the two object's hours, minutes, and seconds, converted into valid hours, minutes and seconds. Prefix and postfix increment operators. These operators should increment the number of hours stored in the object. When incremented, the number of hours may need to be recalculated. ++ Prefix and postfix decrement operators. These operators should decrement the number of hours stored in the object. When decremented, the number of hours may need to be recalculated. Greater than -returns true if the first time is greater than the second Please have your program ask the user for two different time in hours otherwise returns false. otherwise returns false. minutes, and seconds. Then add the two times, subtract the two times, increment the first time, both pre and post increment, and decrement the second time, both pre and post decrement, and compare the two times using (Look at the Feetlnches example.)Explanation / Answer
Time.h:
#ifndef TIME_H
#define TIME_H
#include <iostream>
using namespace std;
class Time
{
public:
Time();
Time(int, int, int);
void setMinutes(int);
void setSeconds(int);
void setHours(int);
void TimeFormat();
friend ostream & operator <<(ostream &out, const Time &temp);
friend istream & operator >>(istream &in, Time &temp);
// overloaded the arithmetic operator.
Time operator+(const Time &b1);
Time operator-(const Time &b1);
Time operator*(const Time &b1);
Time operator/(const Time &b1);
//the prototype to overload the greater than,less than, equality and not-equal-to operator.
int operator>(const Time &b1);
int operator<(const Time &b1);
int operator==(const Time &b1);
int operator!=(const Time &b1);
// the prototype to overload the pre-increment, post-increment, pre-decrement, post-decrement operator.
Time operator++();//prefix increment
Time operator--();//prefix decrement
Time operator++(int);//postfix increment
Time operator--(int);//postfix decrement
private:
int hours;
int minutes;
int seconds;
};
Time.cpp:
#ifndef TIME_CPP
#define TIME_CPP
#include <valarray>
#include "Time.h"
// Default Constructor
Time::Time() {
minutes = 0;
seconds = 0;
hours = 0;
}
// Parameterized Constructor
Time::Time(int h, int m, int s) {
setMinutes(m);
setSeconds(s);
setHours(h);
}
void Time::setMinutes(int m) {
minutes = m;
}
void Time::setSeconds(int s) {
seconds = s;
}
void Time::setHours(int h) {
hours = h;
}
//for formatting thr time:
//if second is greater than 60 minute is incremented by 1
//if minute is greater than 60 then hour is incremented by 1
void Time::TimeFormat() {
if (seconds >= 60) {
minutes = minutes + (seconds / 60);
seconds = seconds % 60;
}
if (minutes >= 60) {
hours = hours + (minutes / 60);
minutes = minutes % 60;
}
}
//insertion
std::ostream& operator<<(std::ostream& out, const Time& temp) {
out << " hours :" << temp.hours;
out << " Minutes :" << temp.minutes;
out << " seconds :" << temp.seconds;
return out;
}
//extraction
std::istream& operator>>(std::istream &in, Time &temp) {
in >> temp.hours;
in >> temp.minutes;
in >> temp.seconds;
return in;
}
// addition operator overload.
Time Time::operator+(const Time &b1) {
Time b3;
b3.seconds = seconds + b1.seconds;
b3.minutes = minutes + b1.minutes;
b3.hours = hours + b1.hours;
b3.TimeFormat();//format the time
return b3;
}
//subtraction operator overload .
Time Time::operator-(const Time &b1) {
Time b3;
b3.minutes = minutes - b1.minutes;
b3.seconds = seconds - b1.seconds;
b3.hours = hours - b1.hours;
b3.TimeFormat();
return b3;
}
// multiplication operator overload
Time Time::operator*(const Time &b1) {
Time b3;
b3.minutes = minutes * b1.minutes;
b3.seconds = seconds * b1.seconds;
b3.hours = hours * b1.hours;
b3.TimeFormat();
return b3;
}
//division operator overload
Time Time::operator/(const Time &b1) {
Time b3;
b3.minutes = minutes / b1.minutes;
b3.seconds = seconds / b1.seconds;
b3.hours = hours / b1.hours;
b3.TimeFormat();
return b3;
}
// Implemented the greater than operator overload.
int Time::operator>(const Time &b1) {
if(hours > b1.hours)
return (1);
if ((hours == b1.hours)&&(seconds > b1.seconds)) {
return (1);
}
if ((hours == b1.hours)&&(seconds == b1.seconds)&&(minutes>b1.minutes)){
return (1);
}
return (0);
}
// less than operator overload .
int Time::operator<(const Time &b1) {
if(hours < b1.hours)
return (1);
if ((hours == b1.hours)&&(seconds < b1.seconds)) {
return (1);
}
if ((hours == b1.hours)&&(seconds == b1.seconds)&&(minutes < b1.minutes)){
return (1);
}
return (0);
}
//equality operator overload.
int Time::operator==(const Time &b1) {
if ((minutes == b1.minutes)&&(seconds == b1.seconds)&&(hours == b1.hours)) {
return (1);
}
return (0);
}
// not-equal-to operator overload.
int Time::operator!=(const Time &b1) {
if ((minutes != b1.minutes)||(seconds != b1.seconds)||(hours != b1.hours)) {
return (1);
}
return (0);
}
// post-increment operator
Time Time::operator++(int) {
Time temp(hours,minutes,seconds);
hours++;
minutes++;
seconds++;
temp.TimeFormat();
return temp;
}
// pre-increment operator overload
Time Time::operator++() {
hours++;
minutes++;
seconds++;
if (seconds >= 60) {
minutes = minutes + (seconds / 60);
seconds = seconds % 60;
}
if (minutes >= 60) {
hours = hours + (minutes / 60);
minutes = minutes % 60;
}
return (*this);
}
// post-decrement operator overload
Time Time::operator--(int) {
Time temp(hours,minutes, seconds);
hours--;
minutes--;
seconds--;
temp.TimeFormat();
return temp;
}
// pre-decrement operator overload
Time Time::operator--() {
hours--;
minutes--;
seconds--;
if (seconds >= 60) {
minutes = minutes + (seconds / 60);
seconds = seconds % 60;
}
if (minutes >= 60) {
hours = hours + (minutes / 60);
minutes = minutes % 60;
}
return (*this);
}
#endif // !TIME_CPP
Main.cpp:
#include "Time.h"
int main()
{
Time Time1;
Time Time2;
//prompt the user for input
cout << " Space separated, enter the hours, seconds, and minutes of Time1: ";
cin >> Time1;
cout << " Space separated, enter the hours, seconds, and minutes of Time2: ";
cin >> Time2;
//display the time and time2
cout << " Time1 :";
cout << Time1;
cout << " Time2 :";
cout << Time2;
//calls the arithmetic operator overloading and prints in the time format
cout << " Time1 + Time2 :: ";
cout << Time1 + Time2;
cout << " Time1 - Time2 :: ";
cout << Time1 - Time2;
cout << " Time1 * Time2 :: ";
cout << Time1 * Time2;
cout << " Time1 / Time2 :: ";
cout << Time1 / Time2;
//calls the greater than and lessthan and prints in time format
if (Time1 > Time2)
cout << " Time1 > Time2 :: Time1 is greater than time 2" << endl;
if (Time1 < Time2)
cout << " Time1 < Time2 :: Time1 is less than Time2 " << endl;
if (Time1 == Time2)
cout << " Time1 == Time2 :: Time1 and Time2 are equal " << endl;
if (Time1 != Time2)
cout << " Time1 != Time2 ::Time1 and Time2 are not equal " << endl;
//increment and decrement the time
cout << " ++Time1 :: ";
cout << ++Time1;
cout << " --Time2 :: " ;
cout << --Time2;
Time time3 = Time2++;
cout << " Time time3 = Time2++;" << endl;
cout << " Time 3 :: ";
cout << time3;
cout << " Time2 :: ";
cout << Time2;
return 0;
}
Sample Run:
Space separated, enter the hours, seconds, and minutes of Time1: 1 20 30
Space separated, enter the hours, seconds, and minutes of Time2: 1 21 30
Time1 : hours :1 Minutes :20 seconds :30
Time2 : hours :1 Minutes :21 seconds :30
Time1 + Time2 :: hours :2 Minutes :42 seconds :0
Time1 - Time2 :: hours :0 Minutes :-1 seconds :0
Time1 * Time2 :: hours :8 Minutes :15 seconds :0
Time1 / Time2 :: hours :1 Minutes :0 seconds :1
Time1 < Time2 :: Time1 is less than Time2
Time1 != Time2 ::Time1 and Time2 are not equal
++Time1 :: hours :2 Minutes :21 seconds :31
--Time2 :: hours :0 Minutes :20 seconds :29
Time time3 = Time2++;
Time 3 :: hours :0 Minutes :20 seconds :29
Time2 :: hours :1 Minutes :21 seconds :30
RUN SUCCESSFUL (total time: 8s)
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.