Question

In: Computer Science

Write the definition for a generic / Template class called time that has hours and minutes...

  1. Write the definition for a generic / Template class called time that has hours and minutes as structure. The class has the following member functions:
    SetTime to set the specified value in object
    ShowTime to display time object
    Sum to sum two time object & return time
  1. Write the definitions for each of the above member functions.
  2. Write main function to create three time objects. Set the value in two objects and call sum() to calculate sum and assign it in third object. Display all time objects.
  3. The language is Visual studio c++

Solutions

Expert Solution

#######################################
            Time.cpp
#######################################
#include "Time.hpp"
#include <sstream>
#include <iomanip>

using namespace std;

Time::Time(int hour, int minute) {
        this->hour = hour;
        this->minute = minute;
}

Time::Time(const Time& time) {
        this->hour = time.hour;
        this->minute = time.minute;
}

void Time::SetTime(int hour, int minute) {
        if(hour < 0 || hour > 23) {
                return;
        }
        if(minute < 0 || minute > 59) {
                return;
        }
        this->hour = hour;
        this->minute = minute;
}

int Time::getHour() const {
        return hour;
}
int Time::getMinute() const {
        return minute;
}

void Time::ShowTime() const {
        cout << toString() << endl;
}

string Time::toString() const {
        stringstream ss;
        ss << setfill('0') << setw(2) << hour;
        ss << ":";
        ss << setfill('0') << setw(2) << minute;

        return ss.str();
}

bool Time::isValid() const {
        return (hour >= 0 && hour < 24 && minute >= 0 && minute < 60);
}

Time Time::Sum(Time &other) const {
        Time result;
        result.minute = minute + other.minute;
        result.hour = hour + other.hour;
        if(result.minute >= 60) {
                result.hour += 1;
                result.minute -= 60;
        }
        return result;
}



#######################################
            Time.hpp
#######################################
#ifndef TIME_HPP
#define TIME_HPP
#include <string>
#include <iostream>
using namespace std;

class Time {
        private:
                int hour;
                int minute;
                
        public:
                Time(int hour = 0, int minute = 0);
                Time(const Time& time);
                
                void SetTime(int hour, int minute);
                
                void ShowTime() const;

                int getHour() const;
                int getMinute() const;
                
                string toString() const;
                bool isValid() const;
                
                Time Sum(Time &other) const;
};
#endif



#######################################
            main.cpp
#######################################
#include <iostream>
#include "Time.hpp"
using namespace std;

int main() {
        Time time1 = Time(1, 25);
        Time time2 = Time(15, 59);
        
        time1.ShowTime();
        time2.ShowTime();

        Time time3 = time1.Sum(time2);
        time3.ShowTime();
        
}



**************************************************

Thanks for your question. We try our best to help you with detailed answers, But in any case, if you need any modification or have a query/issue with respect to above answer, Please ask that in the comment section. We will surely try to address your query ASAP and resolve the issue.

Please consider providing a thumbs up to this question if it helps you. by Doing that, You will help other students, who are facing similar issue.


Related Solutions

Write the definition for a generic class called Rectangle that has data members length and width....
Write the definition for a generic class called Rectangle that has data members length and width. The class has the following member functions: setlength to set the length data member setwidth to set the width data member perimeter to calculate and return the perimeter of the rectangle area to calculate and return the area of the rectangle show to return a text to display the length and width of the rectangle sameArea that has one parameter of type Rectangle. sameArea...
Write the definition for a generic class called Rectangle that has data members length and width....
Write the definition for a generic class called Rectangle that has data members length and width. The class has the following member functions: setlength to set the length data member setwidth to set the width data member perimeter to calculate and return the perimeter of the rectangle area to calculate and return the area of the rectangle show to return a text to display the length and width of the rectangle sameArea that has one parameter of type Rectangle. sameArea...
Write a class called Time that represents the time of the day. It has attributes for...
Write a class called Time that represents the time of the day. It has attributes for the hour and minute. The hour value ranges from 0 to 23, where the range 0 to 11 represents a time before noon. The minute value ranges from 0 to 59. Write a default constructor that initializes the time to 0 hours and 0 minutes. Write a private method isValid(hour, minute) that returns true if the given hour and minute values are in the...
Write a class called Time that represents the time of the day. It has attributes for...
Write a class called Time that represents the time of the day. It has attributes for the hour and minute. The hour value ranges from 0 to 23, where the range 0 to 11 represents a time before noon. The minute value ranges from 0 to 59. Write a default constructor that initializes the time to 0 hours and 0 minutes. Write a private method isValid(hour, minute) that returns true if the given hour and minute values are in the...
Generic types A class or interface that declares one or more generic variables is called a...
Generic types A class or interface that declares one or more generic variables is called a generic type. In this portion of the activity, you will make a class generic. Open GenericsC.java in jGRASP then compile it. At this point you should be familiar with the two type-safety warnings given by the compiler. You should be able to understand the source of the error: the use of the raw types List and Collection. Since the List being declared (al) is...
Write a template class Number with the following features Overload following operators for the template class...
Write a template class Number with the following features Overload following operators for the template class + - < > Overload << and >> operators for the ostream and istream against this class. Write a main function to demonstrate the functionality of each operator.
Write a template class Number with the following features Overload following operators for the template class...
Write a template class Number with the following features Overload following operators for the template class + - < > Overload << and >> operators for the ostream and istream against this class. Write a main function to demonstrate the functionality of each operator.
Using basic C++( without using <Rectangle.h> ) Write the definition for a class called Rectangle that...
Using basic C++( without using <Rectangle.h> ) Write the definition for a class called Rectangle that has floating point data members length and width. The class has the following member functions: void setlength(float) to set the length data member void setwidth(float) to set the width data member float perimeter() to calculate and return the perimeter of the rectangle float area() to calculate and return the area of the rectangle void show() to display the length and width of the rectangle...
Write a C++ code that can convert time into hours, minutes, seconds. It can accept (seconds),...
Write a C++ code that can convert time into hours, minutes, seconds. It can accept (seconds), (minutes, seconds), (hours, minutes, seconds) The input can be written in main It should produce the following output: (67.4, 14, 5) is 67 Hours, 38 Minutes, 5 Seconds (127.86) is 0 Hours, 2 Minutes, 8 Seconds (-3, 73, 2) is -1 Hours, -46 Minutes, -58 Seconds
Write a Python class definition called CellPhone to represent a monthly cell phone bill. The bill...
Write a Python class definition called CellPhone to represent a monthly cell phone bill. The bill should contain the following information: customer name (default value is the empty string) account number (default value is 0) number of gigabytes (GB) used over the monthly limit (default value is 0) Include the following methods: a constructor which given a customer name, account number, and number of GB used in excess of the monthly limit, creates a CellPhone object (be sure to account...
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT