Your assignment is to implement all of the operators without using friends. For
ID: 3913680 • Letter: Y
Question
Your assignment is to implement all of the operators without using friends. For the Boolean and arithmetic operators you should do this by making the operators members of the MyTime class, and for the I/O operators you should do it by writing input and output functions in the class that do the job of the operator, and then have the operator call that member function. The math and Boolean operators should use const to protect the object which is being used as the left-hand operand.
I have also asked that you combine the two constructors into a single constructor by using default arguments.
When you are done you should write an application that allows the user to enter two times, using the overloaded input operator, and a scalar, and then see all the math and 2 of the Boolean operators performed on those numbers.
THE .h FILE
THE .cc FILE
Explanation / Answer
Given below is the modified code for the question.
Please do rate the answer if it was helpful. Thank you
MyTime.h
-----
#include <iostream>
using namespace std;
class MyTime
{
public:
MyTime(int h =0, int m = 0);
void Reset(int h, int m);
MyTime operator + (const MyTime& t2) const;
MyTime operator - (const MyTime& t2) const;
MyTime operator * (int num) const;
MyTime operator / (int num) const;
bool operator == ( const MyTime& t2) const;
bool operator < (const MyTime& t2) const ;
bool operator <= (const MyTime& t2) const;
void input(std::istream& ins);
void output(std::ostream& outs);
int get_hours() const{return hours;}
int get_minutes() const{return minutes;}
private:
void simplify();
int hours; // hours can be > 24
int minutes; // 0 <= minutes <= 59
};
ostream& operator <<(ostream& outs, const MyTime& t1);
istream& operator >> (istream& ins, MyTime& t1);
MyTime.cpp
------
#include "MyTime.h"
#include <cstdlib>
#include <iostream>
#include <iomanip>
using namespace std;
// Constructor
MyTime::MyTime(int h, int m){
Reset(h, m);
}
void MyTime::Reset(int h, int m){
hours = h;
minutes = m;
simplify();
}
void MyTime::simplify(){
hours += minutes/60;
minutes = minutes%60;
}
MyTime MyTime::operator + (const MyTime& t2) const{
MyTime tmp;
tmp.hours = hours + t2.hours;
tmp.minutes = minutes + t2.minutes;
tmp.simplify();
return tmp;
}
MyTime MyTime::operator - (const MyTime& t2) const{
MyTime tmp;
tmp.minutes = abs((hours*60+minutes) -
(t2.hours*60+t2.minutes));
tmp.simplify();
return tmp;
}
MyTime MyTime::operator / ( int num) const{
MyTime tmp;
tmp.minutes = (hours*60 + minutes) / num;
tmp.simplify();
return tmp;
}
MyTime MyTime::operator * ( int num) const{
MyTime tmp;
tmp.minutes = (hours*60 + minutes) * num;
tmp.simplify();
return tmp;
}
bool MyTime::operator == (const MyTime& t2) const{
return hours == t2.hours && minutes == t2.minutes;
}
bool MyTime::operator < ( const MyTime& t2) const {
return (hours*60 + minutes) < (t2.hours*60 + t2.minutes);
}
bool MyTime::operator <=(const MyTime& t2) const{
return (hours*60 + minutes) <= (t2.hours*60 + t2.minutes);
}
void MyTime::input(istream&ins){
// Here you are to copy the implementation code from the >> operator shown below
// remember to that variables will be local here - so no referene to t1
//Then you can have the >> operator call this function.
// In the .h file remove the word friend for the operator and move its prototype to a spot
// under the class declaration
char junk;
ins>>hours;
ins.get(junk);
ins>>minutes;
simplify();
}
void MyTime::output(ostream& outs){
// Do the same thing a you did for the function above except using the code for the <<
//operator
outs<<hours<<':'<<setw(2)<<setfill('0')<<minutes;
}
ostream& operator <<(ostream& outs, const MyTime& t1){
outs<<t1.get_hours()<<':'<<setw(2)<<setfill('0')<<t1.get_minutes();
return outs;
}
istream& operator >> (istream& ins, MyTime& t1){
char junk;
int h, m;
ins>>h;
ins.get(junk);
ins>>m;
t1.Reset(h, m);
return ins;
}
Test.cpp
-------
#include <iostream>
#include "MyTime.h"
#include <string>
using namespace std;
int main(){
MyTime t1;
MyTime t2(15, 30);
cout << "t1 is " << t1 << endl;
cout << "t2 is " << t2 << endl;
MyTime t3, t4;
cout << "Enter time t3 ";
cin >> t3;
cout << "Enter time t4 ";
t4.input(cin);
int scalar1, scalar2;
cout << "t3 is " << t3 << endl;
cout << "t4 is " ;
t4.output(cout);
cout << endl;
cout << "Enter a scalar to multiply time t3: ";
cin >> scalar1;
cout << "Enter a scalar to divide time t4: ";
cin >> scalar2;
cout << "t3 * " << scalar1 << " = " << t3 * scalar1 << endl;
cout << "t4 / " << scalar2 << " = " << t4 / scalar2 << endl;
if(t3 == t4)
cout << t3 << " is equal to " << t4 << endl;
else
cout << t3 << " is NOT equal to " << t4 << endl;
if(t3 < t4)
cout << t3 << " is less than " << t4 << endl;
else
cout << t3 << " is NOT less than " << t4 << endl;
if(t3 <= t4)
cout << t3 << " is less than/equal to " << t4 << endl;
else
cout << t3 << " is NOT less than/equal to " << t4 << endl;
}
output
-----
t1 is 0:00
t2 is 15:30
Enter time t3 5:30
Enter time t4 10:15
t3 is 5:30
t4 is 10:15
Enter a scalar to multiply time t3: 2
Enter a scalar to divide time t4: 3
t3 * 2 = 11:00
t4 / 3 = 3:25
5:30 is NOT equal to 10:15
5:30 is less than 10:15
5:30 is less than/equal to 10:15
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.