Question

Write the definition for a generic class called time that has hours and minutes as structure....

  1. Write the definition for a generic 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. Visual studio c++ is the language.
  4. It should be a generic/template class not a simple class.
0 0
Add a comment Improve this question Transcribed image text
Answer #1

time.cpp

#include<iostream>
using namespace std;

// template class time
template <class T>
class time
{
   // data members
   T hours=0,minutes=0;
   public:
       // function to set time
       void setTime(T hour,T minute)
       {
           hours=hour;
           minutes=minute;
       }
       // function to display time
       void showTime()
       {
           cout<<"Hours: "<<hours<<" Minutes: "<<minutes<<endl;
       }
       // function to add two time objects and return it
       time Sum(time time1,time time2)
       {
           time<int> time3;
           time3.minutes=time1.minutes+time2.minutes;
           if(time3.minutes>=60)
           {
               time3.minutes-=60;
               time3.hours=time1.hours+time2.hours+1;
           }
           else
               time3.hours=time1.hours+time2.hours;
           return time3;
       }
};

// driver function
int main()
{
   // create time object
   time <int>time1;
   // set time to time object
   time1.setTime(10,25);
   time <int>time2;
   time2.setTime(5,45);
   time <int>time3;
   // call sum function to add 2 time objects
   time3=time3.Sum(time1,time2);
   // display all 3 time object
   time1.showTime();
   time2.showTime();
   time3.showTime();
   return 0;
}

Output

CAUsers AKSHAYDesktop\time.exe Hours: 10 Minutes: 25 Hours: 5 Minutes: 45 Hours: 16 Minutes: 10

Add a comment
Know the answer?
Add Answer to:
Write the definition for a generic class called time that has hours and minutes as structure....
Your Answer:

Post as a guest

Your Name:

What's your source?

Earn Coins

Coins can be redeemed for fabulous gifts.

Not the answer you're looking for? Ask your own homework help question. Our experts will answer your question WITHIN MINUTES for Free.
Similar Homework Help Questions
  • The program must be in C# syntax. Write the definition for a generic class called Rectangle...

    The program must be in C# syntax. 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...

  • The program must be in C# syntax. Write the definition for a generic class called Rectangle...

    The program must be in C# syntax. 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...

  • Using separate files, write the definition for a class called Point and a class called Circle. Th...

    Using separate files, write the definition for a class called Point and a class called Circle. The class point has the data members x (double) and y (double) for the x and y coordinates. The class has the following member functions: a) void set_x(double) to set the x data member b) void set_y(double) to set the y data member c) double get_x() to return the the x data member d) double get_y() to return the the y data member e)...

  • Design a class named NumDays, to store a value that represents a number of hours and...

    Design a class named NumDays, to store a value that represents a number of hours and convert it to a number of days. For example, 24 hours would be converted to 1 day, 36 hours would be converted to 1.5 days, and 54 hours would be converted to 2.25 days. This class has two private member variables with data type double: one is named as hours; another is named as days. This class has a constructor that accepts a value...

  • Q1. Write a C++ class called Time24h that describes a time of the day in hours,...

    Q1. Write a C++ class called Time24h that describes a time of the day in hours, minutes, and seconds. It should contain the following methods: • Three overloaded constructors (one of which must include a seconds value). Each constructor should test that the values passed to the constructor are valid; otherwise, it should set the time to be midnight. A display() method to display the time in the format hh:mm:ss. (Note: the time can be displayed as 17:4:34, i.e. leading...

  • 3. write a c++ program: Design and implement a class called Clock that describes the time...

    3. write a c++ program: Design and implement a class called Clock that describes the time of a clock: a) Include in the class 3 constructors with one, two, and three parameters to set the hours, the minutes, and the seconds, respectively. b) Include also a default constructor that sets the member variables of the class to 00:00:00. c) Write a member function to increment the time by a given amount, and second member function to reset the clock. The...

  • U8ing separate files, write th definition for a clas called Poit and a class called Cirele...

    U8ing separate files, write th definition for a clas called Poit and a class called Cirele 1. The class point has the data members (double) and y (double) for the x and y coordinates The class has the following ember functions a) void set x (double) to set the x data member b) void set y(double) to set the y data member c) double get-x() to return th the data member d) double get yO to zeturn the the y...

  • c++ programming language Instructions In this exercise, you will design the class memberType. Each object of...

    c++ programming language Instructions In this exercise, you will design the class memberType. Each object of memberType can hold the name of a person, member ID, number of books bought, and amount spent. Include the member functions to perform the various operations on the objects of memberType—for example, modify, set, and show a person’s name. Similarly, up-date, modify, and show the number of books bought and the amount spent. Add the appropriate constructors. Write the definitions of the member functions...

  • Generics Objectives: OOWorking with a Generic Class 1. Create a class called Node that is Generic...

    java Generics Objectives: OOWorking with a Generic Class 1. Create a class called Node that is Generic. The class has one class attribute that is an element. It will need 2 Constructors, a setter and getter for the class attribute, and a toString method. 2. Write a Lab10Driver that will: Instantiate a Node Integer object with no value. I. Il. Ill. IV. a. Then call the set method to set the value to 5. Instantiate a Node String object with...

  • - Classes Write a C++ program that creates a class called Date that includes three pieces...

    - Classes Write a C++ program that creates a class called Date that includes three pieces of information as data members, namely:  month (type int)  day (type int)  year (type int). Program requirements: - Provide set and a get member functions for each data member. - For the set member functions assume that the values provided for the year and day are correct, but in the setMonth member function ensure that the month value is in the...

ADVERTISEMENT
Free Homework Help App
Download From Google Play
Scan Your Homework
to Get Instant Free Answers
Need Online Homework Help?
Ask a Question
Get Answers For Free
Most questions answered within 3 hours.
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT