Question 1: Is it possible to use a single int private variable minutes that represents minutes since midnight (instead of variables hour and minute) to implement the Time class without changing the interface (i.e., public member functions)? Explain why or why not.
Question 2: List some good reasons for overloading operators in a class.
Question 1)
It is possible to use a single int variable named minutes.
The number of hours can be found out with the help of
minutes.
Consider the minutes value is 361.
The number of hours can be found using minutes/60 which is
361/60=6
The number of hours is 6 and the minutes can be found by minutes%6
which is 361%6=1
So it is possible to use a single int private variable in a Time
class.
Question 2)
Overloading operators:
It is a type of polymorphism where the operator is overloaded and a
new meaning is defined to it.
In order to perform operations on the user defined data type, the
operator overloading can be used.
Consider the operator '+' which can be overloaded.
When the integer parameters are considered it performs addition of
two numbers.
When string type is considered, it concatenates the two
strings.
Get Answers For Free
Most questions answered within 1 hours.