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
RUNTIME INPUT
12
59
45
Output should be
12:59:45
#include <iostream>
using namespace std;
class Time{
int hours, minutes, seconds;
public:
Time(){}
Time(int hrs, int min, int sec){
hours = hrs;
minutes = min;
seconds = sec;
}
void show(){
cout<<this->hours<<":"<<this->minutes<<":"<<this->seconds<<endl;
}
~Time(){
}
};
int main(){
int h, m, s;
cout<<"Enter runtime input\n";
cin>>h;
cin>>m;
cin>>s;
Time time(h, m, s);
cout<<"\noutput\n";
time.show();
return 0;
}
Comments
Leave a comment