Answered step by step
Verified Expert Solution
Link Copied!

Question

1 Approved Answer

Need help with number 6. 4.Create a class Int based on Exercise 1 in Chapter 6. Overload four integer arithmetic operators (+, -, *, and

Need help with number 6.

4.Create a class Int based on Exercise 1 in Chapter 6. Overload four integer arithmetic operators (+, -, *, and /) so that they operate on objects of type Int. If the result of any such arithmetic operation exceeds the normal range of ints (in a 32-bit environment) from 2,147,483,648 to 2,147,483,647have the operator print a warning and terminate the program. Such a data type might be useful where mistakes caused by arithmetic overflow are unacceptable. Hint: To facilitate checking for overflow, perform the calculations using type long double. Write a program to test this class.

5. Augment the time class referred to in Exercise 3 to include overloaded increment (++) and decrement (--) operators that operate in both prefix and postfix notation and return values. Add statements to main() to test these operators

6.Add to the time class of Exercise 5 the ability to subtract two time values using the overloaded (-) operator, and to multiply a time value by a number of type float, using the overloaded (*) operator.

Exercise 5

#include using namespace std; //class time1 definition class time1 { private: int hrs, mins, secs; public: time1(): hrs(0), mins(0), secs(0) //no-arg constructor {} //3-arg constructor time1(int h, int m, int s): hrs(h), mins(m), secs(s) {} void display() //format 11:59:59 { cout << hrs << ":" << mins << ":" << secs; } time1 operator + (time1 t2) //add two times { int s = secs + t2.secs; //add seconds int m = mins + t2.mins; //add minutes int h = hrs + t2.hrs; //add hours if (s > 59) //if secs overflow, { s -= 60; m++; } // carry a minute if (m > 59) //if mins overflow, { m -= 60; h++; } // carry an hour return time1(h, m, s); //return temp value } //opertor overloading function for ++ in pre-increment time1 operator++() { //increment the seconds by 1 secs++; if (secs > 59) //if secs overflow, { secs -= 60; mins++; } // carry a minute if (mins > 59) //if mins overflow, { mins -= 60; hrs++; } // carry an hour return time1(hrs, mins, secs); } //post-increment operator takes a dummy int parameter to avoid the confusion between the two ++ operator overloading functions time1 operator++(int) { secs++; if (secs > 59) //if secs overflow, { secs -= 60; mins++; } // carry a minute if (mins > 59) //if mins overflow, { mins -= 60; hrs++; } // carry an hour return time1(hrs, mins, secs); } time1 operator--() { secs--; if (secs < 0) //if secs underflow, { secs = 59; mins--; } // subtract a minute by 1 if (mins < 0) //if mins underflow, { mins = 59; hrs--; } // subtract a hour by 1 return time1(hrs, mins, secs); } //post-decrement operator takes a dummy int parameter to avoid the confusion between the two -- operator overloading functions time1 operator--(int) { secs--; if (secs < 0) //if secs under`flow, { secs = 59; mins--; } // subtract a minute by 1 if (mins < 0) //if mins underflow, { mins = 59; hrs--; } // subtract an hour by 1 return time1(hrs, mins, secs); } };

int main() { time1 t1(5, 59, 59); //create and initialze time1 t2(4, 30, 30); // two times time1 t3; //create another time t3 = t1 + t2; //add two times cout << " Addition of time t1 and t2 "; cout << " time3 = "; t3.display(); //display result cout << " Pre-increment and Pre-decrement of time t3"; t3 = ++t3; cout << " time3 = "; t3.display(); //display result t3 = --t3; cout << " time3 = "; t3.display(); //display result cout << endl << "Post-increment and Post-decrement of time t4"; time1 t4(5, 58, 0); t4 = --t4; cout << " time4 = "; t4.display(); //display result t4 = t4++; cout << " time4 = "; t4.display(); //display result return 0; }

Step by Step Solution

There are 3 Steps involved in it

Step: 1

blur-text-image

Get Instant Access to Expert-Tailored Solutions

See step-by-step solutions with expert insights and AI powered tools for academic success

Step: 2

blur-text-image

Step: 3

blur-text-image

Ace Your Homework with AI

Get the answers you need in no time with our AI-driven, step-by-step assistance

Get Started

Recommended Textbook for

Modern Database Management

Authors: Jeff Hoffer, Ramesh Venkataraman, Heikki Topi

12th edition

ISBN: 133544613, 978-0133544619

More Books

Students also viewed these Databases questions