Answer to Question #193278 in C++ for Sarivinkumar M

Question #193278

Declare the class Time consisting of data members hours, minutes and seconds. Invoke a null constructor Time() when an object is created, the parameterized constructor Time(hrs, min, sec) to assign values for the data members and show() member function is used to display the time information in format. Finally free the resources of data objects using destructor member function

Run time input

12

20

65


1
Expert's answer
2021-05-16T14:40:32-0400
#include <iostream>
using namespace std;
class Time{
    int *hours, *minutes, *seconds;
    public:
    Time(){}
    Time(int hrs, int min, int sec){
        hours = new int(hrs);
        minutes = new int(min);
        seconds = new int(sec);
        if(*seconds >= 60){
            *seconds -= 60;
            (*minutes)++;
        }
        if(*minutes >= 60){
            *minutes -= 60;
            (*hours)++;
        }
    }
    void show(){
        cout<<*this->hours<<":"<<*this->minutes<<":"<<*this->seconds<<endl;
    }
    ~Time(){
        delete hours;
        delete minutes;
        delete seconds;
    }
};
int main(){
    int h, m, s;
    cout<<"Run time input\n";
    cin>>h;
    cin>>m;
    cin>>s;
    Time time(h, m, s);
    cout<<"\noutput\n";
    time.show();
    return 0;
}

Need a fast expert's response?

Submit order

and get a quick answer at the best price

for any assignment or question with DETAILED EXPLANATIONS!

Comments

No comments. Be the first!

Leave a comment

LATEST TUTORIALS
New on Blog