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...
Write a program that request a time interval in seconds and display it in hours, minutes,...
Write a program that request a time interval in seconds and display it in hours, minutes, second format. (java)
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.
c++ You will implement a template class with the following members: An array of our generic...
c++ You will implement a template class with the following members: An array of our generic type. The size of this array should be determined by an integer argument to the constructor. An integer for storing the array size. A constructor capable of accepting one integer argument. The constructor should initialize the array and set the array size. A method, find Max, that returns the maximum value in the array. A method, find Min, that returns the minimum value in...
1. create a class called ArrayStack that is a generic class. Create a main program to...
1. create a class called ArrayStack that is a generic class. Create a main program to read in one input file and print out the file in reverse order by pushing each item on the stack and popping each item off to print it. The two input files are: tinyTale.txt and numbers.txt. Rules: You cannot inherit the StackofStrings class. 2. Using your new ArrayStack, create a new class called RArrayStack. To do this, you need a) remove the capacity parameter...
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT