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

In my C++ class we are working on this project. I did a lot of the project but I

ID: 3704511 • Letter: I

Question

In my C++ class we are working on this project. I did a lot of the project but I am not good with operator overloading and pointers and need some help.

Functions I need help with:

myClock operator++();

//overload ++ operator as a member function. t++ will increase the seconds by 1.

int operator-(const myClock& t);

//overload - as a member function. t1-t2 returns the difference in seconds between t1 and t2

myClocklmp.cpp Problem 4 myClock.h* a x mainTest.cpp #ifndef MYC LOCK H #define MYCLOCK H #include iostream» 2 using namespace std; //implement a class that mimics a clock. 24 hr clock, ?.e. from 00:00:00 to 23:59:59 9 10 ?class myClock friend ostream& operatorss (ostream&, myClock&); /loverloadt2 returns true if the time represented by t1 is later in the day than t2 public: 19 20 21 //default constructor: sets time to h:m:s (00:00:00 if no parameters passed), // If any of h, m or s is not "legal", then set it to e 23 24 25 26 27 28 29 30 31 void set(int h, int m, int s); //sets the time to h:m:s // If any of h, m or s is not "legal", then set it to e void reset); //resets time to 00:00:00 int operator (const myClock& t); //overload as a member function. t1-t2 returns the difference in seconds between t1 and t2 34 36 37 myClock gprater); /loverload operator as a member function. t++ will increase the seconds by 1 39 40 42 private: int hours; int minutes; int seconds; 43 45 46 47 48 49 #endif#pragma once

Explanation / Answer

Explanation:You have to call operator-() function from main() by writing d=t1-t2; where t1 is calling object and t2 is passed as a parameter to function and function will return time difference in seconds in a integer variable d.
In operator function t1 will available as calling object & t2 is passed as parameter by reference to t.
Note:pointer variable *this is not mandantory

//overload - as a member function. t1-t2 returns the difference in seconds between t1 and t2  
int myClock::operator-(const myClock& t)
{
int ts1,ts2;
//convert time for t1 into seconds
//this pointer having the address of calling object
ts1=this->hours*3600+this->minutes*60+this->seconds;
//convert time for t2 into seconds
ts2=t.hours*3600+t.minutes*60+t.seconds;
return(ts1-ts2); // returns time difference in seconds
}


//overload ++ operator as a member function. t++ will increase the seconds by 1.

Explanation:In operator++ function t will avaiable as calling object

myClock myClock::operator++()
{
// storing current value of time in temp object variable
myClock temp(hours,minutes,seconds);
++temp.seconds; //Incrementing second by 1
if(temp.second > 60)
{++temp.minute;
temp.second -= 60;
}
return temp;
}

Hire Me For All Your Tutoring Needs
Integrity-first tutoring: clear explanations, guidance, and feedback.
Drop an Email at
drjack9650@gmail.com
Chat Now And Get Quote