Q:

C++ program to read time in HH:MM:SS format and convert into total seconds using class

belongs to collection: C++ Classes and Object programs

0

C++ program to read time in HH:MM:SS format and convert into total seconds using class

All Answers

need an explanation for this answer? contact us directly to get an explanation for this answer

Convert time from HH:MM:SS format to seconds using class program in C++

 

/*C++ program to read time in HH:MM:SS format and convert into total seconds.*/
 
#include <iostream>
#include <iomanip>
 
using namespace std;
 
class Time
{
    private:
        int seconds;
        int hh,mm,ss;
    public:
        void getTime(void);
        void convertIntoSeconds(void);
        void displayTime(void);
};
 
void Time::getTime(void)
{
    cout << "Enter time:" << endl;
    cout << "Hours?   ";          cin >> hh;
    cout << "Minutes? ";          cin >> mm;
    cout << "Seconds? ";          cin >> ss;
}
 
void Time::convertIntoSeconds(void)
{
    seconds = hh*3600 + mm*60 + ss;
}
 
void Time::displayTime(void)
{
    cout << "The time is = " << setw(2) << setfill('0') << hh << ":"
                             << setw(2) << setfill('0') << mm << ":"
                             << setw(2) << setfill('0') << ss << endl;
    cout << "Time in total seconds: " << seconds;
}
 
int main()
{
    Time T; //creating objects
     
    T.getTime();
    T.convertIntoSeconds();
    T.displayTime();
     
    return 0;
}

Output

    Enter time:
    Hours?   1
    Minutes? 1
    Seconds? 6
    The time is = 01:01:06
    Time in total seconds: 3666

need an explanation for this answer? contact us directly to get an explanation for this answer

total answers (1)

C++ Classes and Object programs

This question belongs to these collections

Similar questions


need a help?


find thousands of online teachers now
C++ program to demonstrate example of friend funct... >>
<< C++ program to read time in seconds and convert in...