Question

In: Computer Science

Extend the definition of the class clockType by overloading the pre-increment and post-increment operator function as...

Extend the definition of the class clockType by overloading the pre-increment and post-increment operator function as a member of the class clockType.

Write the definition of the function to overload the post-increment operator for the class clockType as defined in the step above.

Main.cpp

//Program that uses the class clockType.

#include <iostream>
#include "newClock.h"

using namespace std;

int main()
{
clockType myClock(5, 6, 23);
clockType yourClock;

cout << "Line 3: myClock = " << myClock << endl;
cout << "Line 4: yourClock = " << yourClock
<< endl;

cout << "Line 5: Enter time in the form "
<< "hrs:mins:secs ";
cin >> myClock;   
cout << "Line 7: New value of myClock = "
<< myClock << endl;   

++myClock;

cout << "Line 9: After increment myClock is "
<< myClock << endl;

yourClock.setTime(13, 35, 38);
cout << "Line 11: Now yourClock = "
<< yourClock << endl;

if (myClock == yourClock)
cout << "Line 13: myClock and yourClock "
<< "are equal" << endl;
else
cout << "Line 15: myClock and yourClock "
<< "are not equal" << endl;

if (myClock <= yourClock)
cout << "Line 17: myClock is less than "
<< "yourClock" << endl;
else
cout << "Line 19: myClock is not less "
<< "than yourClock" << endl;  

cout << "Line 20: Testing post increment operator"
<< endl;

yourClock = myClock++;   

cout << "Line 22: myClock = " << myClock << endl;
cout << "Line 23: yourClock = " << yourClock
<< endl;

return 0;
}

newClock.cpp

//Implementation file newClock.cpp

#include <iostream>
#include "newClock.h"

using namespace std;

//Overload the pre-increment operator.

//Overload the post-increment operator.

//Overload the equality operator.
bool clockType::operator==(const clockType& otherClock) const
{
return (hr == otherClock.hr && min == otherClock.min
&& sec == otherClock.sec);
}

//Overload the not equal operator.
bool clockType::operator!=(const clockType& otherClock) const
{
return (hr != otherClock.hr || min != otherClock.min
|| sec != otherClock.sec);
}

//Overload the less than or equal to operator.
bool clockType::operator<=(const clockType& otherClock) const
{
return ((hr < otherClock.hr) ||
(hr == otherClock.hr && min < otherClock.min) ||
(hr == otherClock.hr && min == otherClock.min &&
sec <= otherClock.sec));
}

//Overload the less than operator.
bool clockType::operator<(const clockType& otherClock) const
{
return ((hr < otherClock.hr) ||
(hr == otherClock.hr && min < otherClock.min) ||
(hr == otherClock.hr && min == otherClock.min &&
sec < otherClock.sec));
}

//Overload the greater than or equal to operator.
bool clockType::operator>=(const clockType& otherClock) const
{
return ((hr > otherClock.hr) ||
(hr == otherClock.hr && min > otherClock.min) ||
(hr == otherClock.hr && min == otherClock.min &&
sec >= otherClock.sec));
}

//Overload the greater than or equal to operator.
bool clockType::operator>(const clockType& otherClock) const
{
return ((hr > otherClock.hr) ||
(hr == otherClock.hr && min > otherClock.min) ||
(hr == otherClock.hr && min == otherClock.min &&
sec > otherClock.sec));
}

void clockType::setTime(int hours, int minutes, int seconds)
{
if (0 <= hours && hours < 24)
hr = hours;
else
hr = 0;

if (0 <= minutes && minutes < 60)
min = minutes;
else
min = 0;

if (0 <= seconds && seconds < 60)
sec = seconds;
else
sec = 0;
}

void clockType::getTime(int& hours, int& minutes,
int& seconds) const
{
hours = hr;
minutes = min;
seconds = sec;
}

//Constructor
clockType::clockType(int hours, int minutes, int seconds)
{
setTime(hours, minutes, seconds);
}

//Overload the stream insertion operator.
ostream& operator<<(ostream& osObject, const clockType& timeOut)
{
if (timeOut.hr < 10)
osObject << '0';
osObject << timeOut.hr << ':';
  
if (timeOut.min < 10)
osObject << '0';
osObject << timeOut.min << ':';
  
if (timeOut.sec < 10)
osObject << '0';
osObject << timeOut.sec;

return osObject; //return the ostream object
}

//overload the stream extraction operator
istream& operator>> (istream& is, clockType& timeIn)
{
char ch;

is >> timeIn.hr; //Step a

if (timeIn.hr < 0 || timeIn.hr >= 24) //Step a
timeIn.hr = 0;
  
is.get(ch); //Read and discard :. Step b

is >> timeIn.min; //Step c

if (timeIn.min < 0 || timeIn.min >= 60) //Step c
timeIn.min = 0;

is.get(ch); //Read and discard :. Step d

is >> timeIn.sec; //Step e

if (timeIn.sec < 0 || timeIn.sec >= 60) //Step e
timeIn.sec = 0;

return is; //Step f
}

newClock.h

//Header file newClock.h

#ifndef H_newClock
#define H_newClock

#include <iostream>

using namespace std;

class clockType
{
friend ostream& operator<<(ostream&, const clockType&);
friend istream& operator>>(istream&, clockType&);

public:
void setTime(int hours, int minutes, int seconds);
//Function to set the member variables hr, min, and sec.
//Postcondition: hr = hours; min = minutes; sec = seconds

void getTime(int& hours, int& minutes, int& seconds) const;
//Function to return the time.
//Postcondition: hours = hr; minutes = min; seconds = sec

clockType operator++();
//Overload the pre-increment operator.
//Postcondition: The time is incremented by one second.

   clockType operator++(int);
   // Overload the post increment operator
   // Postcondition: Time is incremented by one second.

bool operator==(const clockType& otherClock) const;
//Overload the equality operator.
//Postcondition: Returns true if the time of this clock
// is equal to the time of otherClock,
// otherwise it returns false.

bool operator!=(const clockType& otherClock) const;
//Overload the not equal operator.
//Postcondition: Returns true if the time of this clock
// is not equal to the time of otherClock,
// otherwise it returns false.

bool operator<=(const clockType& otherClock) const;
//Overload the less than or equal to operator.
//Postcondition: Returns true if the time of this clock
// is less than or equal to the time of
// otherClock, otherwise it returns false.

bool operator<(const clockType& otherClock) const;
//Overload the less than operator.
//Postcondition: Returns true if the time of this clock
// is less than the time of otherClock,
// otherwise it returns false.

bool operator>=(const clockType& otherClock) const;
//Overload the greater than or equal to operator.
//Postcondition: Returns true if the time of this clock
// is greater than or equal to the time of
// otherClock, otherwise it returns false.

bool operator>(const clockType& otherClock) const;
//Overload the greater than operator.
//Postcondition: Returns true if the time of this clock
// is greater than the time of otherClock,
// otherwise it returns false.

clockType(int hours = 0, int minutes = 0, int seconds = 0);
//Constructor to initialize the object with the values
//specified by the user. If no values are specified,
//the default values are assumed.
//Postcondition: hr = hours; min = minutes;
// sec = seconds;

private:
int hr; //variable to store the hours
int min; //variable to store the minutes
int sec; //variable to store the seconds
};

#endif

Solutions

Expert Solution

//newClock.h

#ifndef H_newClock
#define H_newClock

#include <iostream>

using namespace std;

class clockType
{
friend ostream& operator<<(ostream&, const clockType&);
friend istream& operator>>(istream&, clockType&);

public:
void setTime(int hours, int minutes, int seconds);
//Function to set the member variables hr, min, and sec.
//Postcondition: hr = hours; min = minutes; sec = seconds

void getTime(int& hours, int& minutes, int& seconds) const;
//Function to return the time.
//Postcondition: hours = hr; minutes = min; seconds = sec

clockType operator++();
//Overload the pre-increment operator.
//Postcondition: The time is incremented by one second.

clockType operator++(int);
// Overload the post increment operator
// Postcondition: Time is incremented by one second.

bool operator==(const clockType& otherClock) const;
//Overload the equality operator.
//Postcondition: Returns true if the time of this clock
// is equal to the time of otherClock,
// otherwise it returns false.

bool operator!=(const clockType& otherClock) const;
//Overload the not equal operator.
//Postcondition: Returns true if the time of this clock
// is not equal to the time of otherClock,
// otherwise it returns false.

bool operator<=(const clockType& otherClock) const;
//Overload the less than or equal to operator.
//Postcondition: Returns true if the time of this clock
// is less than or equal to the time of
// otherClock, otherwise it returns false.

bool operator<(const clockType& otherClock) const;
//Overload the less than operator.
//Postcondition: Returns true if the time of this clock
// is less than the time of otherClock,
// otherwise it returns false.

bool operator>=(const clockType& otherClock) const;
//Overload the greater than or equal to operator.
//Postcondition: Returns true if the time of this clock
// is greater than or equal to the time of
// otherClock, otherwise it returns false.

bool operator>(const clockType& otherClock) const;
//Overload the greater than operator.
//Postcondition: Returns true if the time of this clock
// is greater than the time of otherClock,
// otherwise it returns false.

clockType(int hours = 0, int minutes = 0, int seconds = 0);
//Constructor to initialize the object with the values
//specified by the user. If no values are specified,
//the default values are assumed.
//Postcondition: hr = hours; min = minutes;
// sec = seconds;

private:
int hr; //variable to store the hours
int min; //variable to store the minutes
int sec; //variable to store the seconds
};

#endif

//newClock.cpp

#include <iostream>
#include "newClock.h"

using namespace std;

clockType clockType::operator++(){
   int s = this->sec + 1;
   int m = this->min + s/60;
   int h = this->hr + m/60;
  
   s%=60;
   m%=60;
   h%=24;
  
   this->sec = s;
   this->min = m;
   this->hr = h;
   return *this;
}
//Overload the pre-increment operator.
//Postcondition: The time is incremented by one second.

clockType clockType::operator++(int ){
  
   int s = this->sec + 1;
   int m = this->min + s/60;
   int h = this->hr + m/60;
  
   s%=60;
   m%=60;
   h%=24;
  
   const clockType old(h,m,s);

return old;
}

//Overload the equality operator.
bool clockType::operator==(const clockType& otherClock) const
{
return (hr == otherClock.hr && min == otherClock.min
&& sec == otherClock.sec);
}

//Overload the not equal operator.
bool clockType::operator!=(const clockType& otherClock) const
{
return (hr != otherClock.hr || min != otherClock.min
|| sec != otherClock.sec);
}

//Overload the less than or equal to operator.
bool clockType::operator<=(const clockType& otherClock) const
{
return ((hr < otherClock.hr) ||
(hr == otherClock.hr && min < otherClock.min) ||
(hr == otherClock.hr && min == otherClock.min &&
sec <= otherClock.sec));
}

//Overload the less than operator.
bool clockType::operator<(const clockType& otherClock) const
{
return ((hr < otherClock.hr) ||
(hr == otherClock.hr && min < otherClock.min) ||
(hr == otherClock.hr && min == otherClock.min &&
sec < otherClock.sec));
}

//Overload the greater than or equal to operator.
bool clockType::operator>=(const clockType& otherClock) const
{
return ((hr > otherClock.hr) ||
(hr == otherClock.hr && min > otherClock.min) ||
(hr == otherClock.hr && min == otherClock.min &&
sec >= otherClock.sec));
}

//Overload the greater than or equal to operator.
bool clockType::operator>(const clockType& otherClock) const
{
return ((hr > otherClock.hr) ||
(hr == otherClock.hr && min > otherClock.min) ||
(hr == otherClock.hr && min == otherClock.min &&
sec > otherClock.sec));
}

void clockType::setTime(int hours, int minutes, int seconds)
{
if (0 <= hours && hours < 24)
hr = hours;
else
hr = 0;

if (0 <= minutes && minutes < 60)
min = minutes;
else
min = 0;

if (0 <= seconds && seconds < 60)
sec = seconds;
else
sec = 0;
}

void clockType::getTime(int& hours, int& minutes,
int& seconds) const
{
hours = hr;
minutes = min;
seconds = sec;
}

//Constructor
clockType::clockType(int hours, int minutes, int seconds)
{
setTime(hours, minutes, seconds);
}

//Overload the stream insertion operator.
ostream& operator<<(ostream& osObject, const clockType& timeOut)
{
if (timeOut.hr < 10)
osObject << '0';
osObject << timeOut.hr << ':';
  
if (timeOut.min < 10)
osObject << '0';
osObject << timeOut.min << ':';
  
if (timeOut.sec < 10)
osObject << '0';
osObject << timeOut.sec;

return osObject; //return the ostream object
}

//overload the stream extraction operator
istream& operator>> (istream& is, clockType& timeIn)
{
char ch;

is >> timeIn.hr; //Step a

if (timeIn.hr < 0 || timeIn.hr >= 24) //Step a
timeIn.hr = 0;
  
is.get(ch); //Read and discard :. Step b

is >> timeIn.min; //Step c

if (timeIn.min < 0 || timeIn.min >= 60) //Step c
timeIn.min = 0;

is.get(ch); //Read and discard :. Step d

is >> timeIn.sec; //Step e

if (timeIn.sec < 0 || timeIn.sec >= 60) //Step e
timeIn.sec = 0;

return is; //Step f
}

/Main.cpp

#include <iostream>
#include "newClock.cpp"

using namespace std;

int main()
{
clockType myClock(5, 6, 23);
clockType yourClock;

cout << "Line 3: myClock = " << myClock << endl;
cout << "Line 4: yourClock = " << yourClock
<< endl;

cout << "Line 5: Enter time in the form "
<< "hrs:mins:secs ";
cin >> myClock;   
cout << "Line 7: New value of myClock = "
<< myClock << endl;   

++myClock;

cout << "Line 9: After increment myClock is "
<< myClock << endl;

yourClock.setTime(13, 35, 38);
cout << "Line 11: Now yourClock = "
<< yourClock << endl;

if (myClock == yourClock)
cout << "Line 13: myClock and yourClock "
<< "are equal" << endl;
else
cout << "Line 15: myClock and yourClock "
<< "are not equal" << endl;

if (myClock <= yourClock)
cout << "Line 17: myClock is less than "
<< "yourClock" << endl;
else
cout << "Line 19: myClock is not less "
<< "than yourClock" << endl;

cout << "Line 20: Testing post increment operator"
<< endl;

yourClock = myClock++;   

cout << "Line 22: myClock = " << myClock << endl;
cout << "Line 23: yourClock = " << yourClock
<< endl;

return 0;
}

//sample output


Related Solutions

c++ using class... define operator overloading and give simple example how we can use operator overloading...
c++ using class... define operator overloading and give simple example how we can use operator overloading by writing simple program in which different operators are used to add, subtract, multiply and division.
Overloading the insertion (<<) and extraction (>>) operators for class use requires creating operator functions that...
Overloading the insertion (<<) and extraction (>>) operators for class use requires creating operator functions that use these symbols but have a parameter list that includes a class ____. object address reference data type Flag this Question Question 210 pts When overloading the insertion operator to process a Complex object, it’s important to understand that you’re overloading an operator in the ____ class. istream operator Complex ostream Flag this Question Question 310 pts ____ class variables are allocated memory locations...
Complete the following: Extend the newString class (attached) to include the following: Overload the operator +...
Complete the following: Extend the newString class (attached) to include the following: Overload the operator + to perform string concatenation. Overload the operator += to work as shown here: s1 = "Hello " s2 = "there" s1 += s2 // Should assign "Hello there" to s1 Add a function length to return the length of the string. Write a test program. //myString.h (header file) //Header file myString.h    #ifndef H_myString #define H_myString #include <iostream> using namespace std; class newString {...
Describe in detail the chemicals used in the pre-treatment, post-increment and membrane cleaning (CIP) stages used...
Describe in detail the chemicals used in the pre-treatment, post-increment and membrane cleaning (CIP) stages used in desalination plants where ro membranes are used, and their intended use. (do not copy others answers on chegg if you don't want to negative score)
What do you understand by pre- and post-conditions of a function? Write the pre- and post-conditions...
What do you understand by pre- and post-conditions of a function? Write the pre- and post-conditions to axiomatically specify the following functions: (a) A function takes two floating point numbers representing the sides of a rectangle as input and returns the area of the corresponding rectangle as output. (b) A function accepts three integers in the range of -100 and +100 and determines the largest of the three integers. (c) A function takes an array of integers as input and...
Class object in C++ programming language description about lesson Overloading function example.
Class object in C++ programming language description about lesson Overloading function example.
Class object in C++ programming language description about lesson unary overloading function example.
Class object in C++ programming language description about lesson unary overloading function example.
1. Can you extend an abstract class? In what situation can you not inherit/extend a class?...
1. Can you extend an abstract class? In what situation can you not inherit/extend a class? 2. Can you still make it an abstract class if a class does not have any abstract methods?
Please write a C++ program. Please rewrite your Array (including the operator overloading) into a template....
Please write a C++ program. Please rewrite your Array (including the operator overloading) into a template. And rewrite your main function to test your template for integer array and double array. Following is my complete code: #include <iostream> using namespace std; class Array { private: // Pointer to memory block to store integers int* data; // Maximum size of memory block int cap; // Stores number of integers in an array int num; public: // Constructor Array(int size); // Default...
C++ Programming 19.2 Operator Overloading practice Write the prototypes and functions to overload the given operators...
C++ Programming 19.2 Operator Overloading practice Write the prototypes and functions to overload the given operators in the code main.cpp //This program shows how to use the class rectangleType. #include <iostream> #include "rectangleType.h" using namespace std; int main() { rectangleType rectangle1(23, 45); //Line 1 rectangleType rectangle2(12, 10); //Line 2 rectangleType rectangle3; //Line 3 rectangleType rectangle4; //Line 4 cout << "Line 5: rectangle1: "; //Line 5 rectangle1.print(); //Line 6 cout << endl; //Line 7 cout << "Line 8: rectangle2: "; //Line...
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT