Home »
C++ Programs
C++ program to convert the minutes to time using class
Submitted by Shubh Pachori, on September 01, 2022
Problem statement
Given numbers, we have to convert the minutes to time using the class and object approach.
Example:
Input:
Enter Minutes: 120
Output:
Hour : Minute = 2 : 0
C++ code to convert the minutes to time using the class and object approach
#include <iostream>
#include <string>
using namespace std;
// create a class
class Time {
// private data members
private:
int minute;
// public member functions
public:
// getMinutes() function to insert minutes
void getMinutes() {
cout << "Enter Minutes:";
cin >> minute;
}
// getTime() function to convert minutes into time
void getTime() {
// initializing bool, int type variables to perform
// operations and manipulations
bool condition;
int hour = 0;
// do while loop to convert the inputted minutes to the time
do {
// making condition false
condition = false;
// if condition to check if the minutes
// are greater than equal to 60
if (minute >= 60) {
// increment of 1 in hour
hour++;
// 60 is subtracted from minute
minute = minute - 60;
// making condition true
condition = true;
}
// while condition to stop the loop
} while (condition);
cout << "Hour : Minute = " << hour << " : " << minute << endl;
}
};
int main() {
// create an object
Time T;
// calling getMinutes() function to
// insert minutes
T.getMinutes();
// getTime() function to convert
// inputted minutes to time
T.getTime();
return 0;
}
Output
Enter Minutes: 139
Hour : Minute = 2 : 19
Explanation
In the above code, we have created a class Time, one int type data member minute to store the minutes, and public member functions getMinutes() and getTime() to store minutes and to convert minutes to time.
In the main() function, we are creating an object T of class Time, reading minutes by the user using getMinutes() function, and finally calling the getTime() member function to convert minutes to time. The getTime() function contains the logic to convert minutes to time and printing the result.
C++ Class and Object Programs (Set 2) »