Question

Writing Overloading Operators [ Time.h, Time.cpp, and TimeDriver.cpp ] Purpose. The purpose of this lab is...

Writing Overloading Operators [ Time.h, Time.cpp, and TimeDriver.cpp ]
Purpose. The purpose of this lab is for you to learn how to create and apply the overloaded operators commonly used in C++ data structures.

Requirements. Modify the 3 lab 1b files: Time.h, Time.cpp, and TimeDriver.cpp by adding overloaded operators. Add these overloaded operators:

< (less-than), comparing two Time objects based on their total time values -- 3600*hours + 60*minutes + seconds

== (equals), comparing their total time values -- not

You may write these overloaded operator functions as getter member functions or stand-alone, as you wish. Put any stand-alone prototypes in Time.h, AFTER the closing curly brace and semicolon of the class definition, and ABOVE the closing #endif. Put any stand-alone function definitions in Time.cpp WITHOUT a Time:: scope resolution.

Modify the driver CPP so that it fully tests your overloaded operators.

Program I/O. Input: All hard-coded in the driver CPP only. Output: From the driver CPP only, console (cout) output only.

0 0
Add a comment Improve this question Transcribed image text
Answer #1

Here is the code for Time.h:

class Time
{
public:
Time( int, int, int ); //Initializes a Time object

void setHour( int ); //Set hour data member to a value between 1 and 12
void setMinute( int ); //Set minute data member to a value between 0 and 59
void setSecond( int ); //Set second data member to a value between 0 and 59

int getHour(); //Returns the contents of the hour data member
int getMinute(); //Returns the contents of the minute data member
int getSecond(); //Returns the contents of the second data member

void printTime(); //Prints the contents of a Time object (hour:minute:second)
bool operator<(Time t) const;   //Returns true if first time is less than second time.
bool operator==(Time t) const; //Returns true if both times are equal.

private:
int hour, minute, second;
};

And the code for Time.cpp is:

#include <iostream>
#include <iomanip>
#include "Time.h"
using namespace std;
  
Time::Time(int hours, int minutes, int seconds)
{
setHour(hours);
setMinute(minutes);
setSecond(seconds);
}
void Time::setHour(int hours)
{
if(hours < 1 || hours > 12)
hours = 12;
else
hour = hours;
}
void Time::setMinute(int minutes)
{
if(minutes < 1 || minutes > 60)
minute = 60;
else
minute = minutes;
}
void Time::setSecond(int seconds)
{
if(seconds < 1 || seconds > 60)
seconds = 60;
else
second = seconds;
}
int Time::getHour()
{
return hour;
}
int Time::getMinute()
{
return minute;
}
int Time::getSecond()
{
return second;
}   
void Time::printTime()
{
cout<<setfill('0')<<setw(2)<<getHour()<<":"<<setfill('0')<<setw(2)<<getMinute()<<":"<<setfill('0')<<setw(2)<<getSecond();
}
bool Time::operator<(Time t) const
{
if((3600*hour + 60*minute + second) < (3600*t.getHour() + 60*t.getMinute() + t.getSecond()))
return true;
return false;
}
bool Time::operator==(Time t) const
{
if(hour == t.getHour() && minute == t.getMinute() && second == t.getSecond())
return true;
return false;
}
  



And finally TimeDriver.cp is:

#include "Time.cpp"
int main()
{
Time time1 (10, 3, 28);
Time time2 (9, 8, 10);
Time time3 (11, 50, 10);
cout<<"The first time is: ";
time1.printTime();
cout<<endl<<"The second time is: ";
time2.printTime();
cout<<endl<<"The third time is: ";
time3.printTime();
cout<<endl;
if(time1 < time2)
{
time1.printTime();
cout<<" is less than ";
time2.printTime();
cout<<endl;
}
else
{
time2.printTime();
cout<<" is less than ";
time1.printTime();
cout<<endl;
}
if(time1 == time2)
{
time1.printTime();
cout<<" is equal than ";
time2.printTime();
cout<<endl;
}
else
{
time2.printTime();
cout<<" is not equal than ";
time1.printTime();
cout<<endl;
}
}

And the output screenshot is:

Terminal Shell Edit View Window Help Θ < > f) 95% B ), Thu 8 Sep 16:32 ANANDA UMMAPUDI a E GTimeDriver.cpp Currently Open Doc

If you need any refinements, just get back to me.

Add a comment
Know the answer?
Add Answer to:
Writing Overloading Operators [ Time.h, Time.cpp, and TimeDriver.cpp ] Purpose. The purpose of this lab is...
Your Answer:

Post as a guest

Your Name:

What's your source?

Earn Coins

Coins can be redeemed for fabulous gifts.

Not the answer you're looking for? Ask your own homework help question. Our experts will answer your question WITHIN MINUTES for Free.
Similar Homework Help Questions
  • Overview This checkpoint is intended to help you practice the syntax of operator overloading with member...

    Overview This checkpoint is intended to help you practice the syntax of operator overloading with member functions in C++. You will work with the same money class that you did in Checkpoint A, implementing a few more operators. Instructions Begin with a working (but simplistic) implementation of a Money class that contains integer values for dollars and cents. Here is my code: /********************* * File: check12b.cpp *********************/ #include using namespace std; #include "money.h" /**************************************************************** * Function: main * Purpose: Test...

  • Need C++ coding for this lab exercise given below :) 4. Lab Exercise — Templates Exercise...

    Need C++ coding for this lab exercise given below :) 4. Lab Exercise — Templates Exercise 1 - Function Templating For this part of the lab make a template out of the myMax function and test it on different data types. Copy maxTemplate.cpp to your Hercules account space. Do that by: Entering the command:cp /net/data/ftp/pub/class/115/ftp/cpp/Templates/maxTemplate.cpp maxTemplate.cpp Compile and run the program to see how it works. Make a template out of myMax. Don't forget the return type. Modify the prototype appropriately. Test your myMax template on int, double,...

  • please help with the operator overloading lab (intArray) in c++ will provide what it is being...

    please help with the operator overloading lab (intArray) in c++ will provide what it is being required and the code that was given from the book. the code that was provided is below ------------------------------------------------------------------------------------------------------------------------- // iadrv.h #ifndef _IADRV_H #define _IADRV_H #include "intarray.h" int main(); void test1(); void test2(); void test3(); void test4(); void test5(); void test6(); void test7(); void test8(); void test9(); void test10(); void test11(); void test12(); void test13(); void test14(); void test15(); void test16(); void test17(); void test18();...

  • Please use Java to write the program The purpose of this lab is to practice using...

    Please use Java to write the program The purpose of this lab is to practice using inheritance and polymorphism. We need a set of classes for an Insurance company. Insurance companies offer many different types of policies: car, homeowners, flood, earthquake, health, etc. Insurance policies have a lot of data and behavior in common. But they also have differences. You want to build each insurance policy class so that you can reuse as much code as possible. Avoid duplicating code....

  • #include<stdio.h> #include<stdlib.h> #include <time.h> int main() { /* first you have to let the computer generate...

    #include<stdio.h> #include<stdlib.h> #include <time.h> int main() { /* first you have to let the computer generate a random number. Then it has to declare how many tries the user has for the game loop. we then need the player to enter their guess. After every guess we have to give an output of how many numbers they have in the right location and how many they have the right number. The player will keep guessing until their 10 tries are...

  • SCREENSHOTS ONLY PLEASE!!! DON'T POST ACTUAL CODE PLEASE LEAVE A SCREENSHOT ONLY! ACTUAL TEXT IS NOT NEEDED!!! myst...

    SCREENSHOTS ONLY PLEASE!!! DON'T POST ACTUAL CODE PLEASE LEAVE A SCREENSHOT ONLY! ACTUAL TEXT IS NOT NEEDED!!! mystring.h: //File: mystring1.h // ================ // Interface file for user-defined String class. #ifndef _MYSTRING_H #define _MYSTRING_H #include<iostream> #include <cstring> // for strlen(), etc. using namespace std; #define MAX_STR_LENGTH 200 class String { public: String(); String(const char s[]); // a conversion constructor void append(const String &str); // Relational operators bool operator ==(const String &str) const; bool operator !=(const String &str) const; bool operator >(const...

  • This C++ Program consists of: operator overloading, as well as experience with managing dynamic memory allocation...

    This C++ Program consists of: operator overloading, as well as experience with managing dynamic memory allocation inside a class. Task One common limitation of programming languages is that the built-in types are limited to smaller finite ranges of storage. For instance, the built-in int type in C++ is 4 bytes in most systems today, allowing for about 4 billion different numbers. The regular int splits this range between positive and negative numbers, but even an unsigned int (assuming 4 bytes)...

  • This is a standard C++ programming assignment using a command line g++ compiler or the embedded...

    This is a standard C++ programming assignment using a command line g++ compiler or the embedded one in zyBooks. No GUI forms and no Visual Studio. No external files and no databases are used. There is only one object-oriented program to complete in this assignment. All code should be saved in a file named ExamScoresUpdate.cpp, which is the only file to submit for grading. No .h files used. The .cpp file contains main() and two classes, ExamScores and DataCollector. There...

  • 18.1 Lab Lesson 11 (Part 1 of 1) Part of lab lesson 11 There in one...

    18.1 Lab Lesson 11 (Part 1 of 1) Part of lab lesson 11 There in one part to lab lesson 11. The entire lab will be worth 100 points. Lab lesson 11 part 1 is worth 100 points For part 1 you will have 80 points if you enter the program and successfully run the program tests. An additional 20 points will be based on the style and formatting of your C++ code. Style points The 20 points for coding...

ADVERTISEMENT
Free Homework Help App
Download From Google Play
Scan Your Homework
to Get Instant Free Answers
Need Online Homework Help?
Ask a Question
Get Answers For Free
Most questions answered within 3 hours.
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT