Question

C++ program Create a class Time having data members HH, MM, SS of type integer. Write...

C++ program

Create a class Time having data members HH, MM, SS of type integer.

Write a C++ program to do the following:

1. Use a Constructor to initialize HH, MM, SS to 0.

2. Create two Objects of this class and read the values of HH, MM, SS from the user for both objects (Using proper member functions).

3. Use a binary + operator to add these two objects created (Store & display result using separate object).

4. Use a Destructor to destroy the objects created.   

0 0
Add a comment Improve this question Transcribed image text
Answer #1

CODE:-

#include <iostream>

using namespace std;

class Time{ // this is the time class

public:

int HH; // class variables

int MM;

int SS;

Time(){ //constructor

HH = 0;

SS = 0;

MM = 0;

}

Time operator + (Time &obj){ // overloading the + operator to use it for objects of the time class

Time t;

t.HH = HH + obj.HH;

t.MM = MM + obj.MM;

t.SS = SS + obj.SS;

return t;

}

~Time(){ // this the destructor

cout<< "Object destructed"<<endl;

}

};


int main() {

Time t1;

t1.HH = 11;

t1.MM = 20;

t1.SS = 45;

Time t2;

t2.HH = 10;

t2.MM = 15;

t2.SS = 30;

Time t3 = t1+t2;

cout<<t3.HH<<" "<<t3.MM<<" "<<t3.SS<<endl;

}

FOR ANY OTHER QUERY PLEASE COMMENT

Add a comment
Know the answer?
Add Answer to:
C++ program Create a class Time having data members HH, MM, SS of type integer. Write...
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
  • Write a program

    1. Write a program which have a class named as “Student”, while the data members of the class are,a. char name[10],b. int age;c. string color;d. string gender;e. double cgpa;and the member function “print()”,  is just to display these information.1. You need to create 3 objects of the “Student” class.2. Initialize one object by the default constructor having some static values.3. Initialize the second object by the parameterized constructor, while you need to pass values for initialization.4. Initialize the third object...

  • About Classes and OOP in C++ Part 1 Task 1: Create a class called Person, which...

    About Classes and OOP in C++ Part 1 Task 1: Create a class called Person, which has private data members for name, age, gender, and height. You MUST use this pointer when any of the member functions are using the member variables. Implement a constructor that initializes the strings with an empty string, characters with a null character and the numbers with zero. Create getters and setters for each member variable. Ensure that you identify correctly which member functions should...

  • C++ program Create a Student class that contains three private data members of stududentID (int), lastName...

    C++ program Create a Student class that contains three private data members of stududentID (int), lastName (string), firstName(string) and a static data member studentCount(int). Create a constructor that will take three parameters of studentID, lastName and firstName, and assign them to the private data member. Then, increase the studentCount in the constructor. Create a static function getStudentCount() that returns the value of studentCount. studentCount is used to track the number of student object has been instantiated. Initialize it to 0...

  • Construct a class named Fractions containing two integer data members named num and denom, used to...

    Construct a class named Fractions containing two integer data members named num and denom, used to store the numerator and denominator of a fraction having the form num/denom. Your class should include a default constructor that initializes num and denom to 1 if there's no user initialization, and it must prohibit a 0 denominator value. In addition, create member functions for displaying an object's data values and overloaded operator functions for adding, subtracting, multiplying, and dividing two Fraction objects, as...

  • Part (A) Note: This class must be created in a separate cpp file Create a class...

    Part (A) Note: This class must be created in a separate cpp file Create a class Employee with the following attributes and operations: Attributes (Data members): i. An array to store the employee name. The length of this array is 256 bytes. ii. An array to store the company name. The length of this array is 256 bytes. iii. An array to store the department name. The length of this array is 256 bytes. iv. An unsigned integer to store...

  • In C++, Step 1: Implement the Student Class and write a simple main() driver program to...

    In C++, Step 1: Implement the Student Class and write a simple main() driver program to instantiate several objects of this class, populate each object, and display the information for each object. The defintion of the student class should be written in an individual header (i.e. student.h) file and the implementation of the methods of the student class should be written in a corresponding source (i.e. student.cpp) file. Student class - The name of the class should be Student. -...

  • In C++ Write a program that contains a class called VideoGame. The class should contain the...

    In C++ Write a program that contains a class called VideoGame. The class should contain the member variables: Name price rating Specifications: Dynamically allocate all member variables. Write get/set methods for all member variables. Write a constructor that takes three parameters and initializes the member variables. Write a destructor. Add code to the destructor. In addition to any other code you may put in the destructor you should also add a cout statement that will print the message “Destructor Called”....

  • 1. Define a class Rectangle with two attributes: (This is for C++) -length, an integer (default...

    1. Define a class Rectangle with two attributes: (This is for C++) -length, an integer (default value 1) -width, an integer (default value 1) Provide the following member functions: -default constructor -constructor that takes parameters used to initialize the data -get/set function for each attribute -a function perimeter that returns the perimeter of the rectangle -a function area that returns the area of the rectangle b. Write a program that uses the class Rectangle to create two rectangle objects with...

  • Create the program in C++ please. The new operator as outlined in 10.9 . Create a...

    Create the program in C++ please. The new operator as outlined in 10.9 . Create a Test class that includes the following Data members • First Name, last name, test1, test2, test3 o Methods • Accessor methods for each of the data members' • Mutator methods for each of the data members . A Default Constructor . A constructor that will accept arguments for each of the 5 data members • A method that will calculate the average of the...

  • Write a code in C++ by considering the following conditions :- Tasks :- 1. Create a...

    Write a code in C++ by considering the following conditions :- Tasks :- 1. Create a class Employee (with member variables: char * name, int id, and double age). 2. Create a constructor that should be able to take arguments and initialize the member variables. 3. Create a copy constructor for employee class to ensure deep copy. 4. Create a destructor and de allocate the dynamic memory. 5. Overload the assignment operator to prevent shallow copy and ensure deep copy....

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