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
#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;
}
Comments
Leave a comment