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.
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:

If you need any refinements, just get back to me.
Writing Overloading Operators [ Time.h, Time.cpp, and TimeDriver.cpp ] Purpose. The purpose of this lab is...
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...
I need help implementing 3 function prototypes with using operator overloading in a class. I have bolded the section I need help with since the cpp file is bigger than I expected. Any comments throughout would be extremely helpful. Time.cpp file #include "Time.h" #include #include #include // The class name is Time. This defines a class for keeping time in hours, minutes, and AM/PM indicator. // You should create 3 private member variables for this class. An integer variable for...
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 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 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....
C++ I am using visual studio for it. Make a copy of the Rational class you created in the previous Lab. Modify the class. Replace all your mathematical, input, and output functions with overloaded operators. Overload the following 12 operators: + - * / < > = = ! = <= >= >> << In order to test your class in your main function, prompt the user for a numerator and a denominator for your first object. Repeat the prompt...
#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!!!
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 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)...