In: Computer Science
Question:
Use Eclipse to create a clockType with hr, min, sec as private members.
You shall have 3 files: clock.h clock.cpp and lab1main.cpp
lab1main.cpp shall support the following statements:
clockType c1(15, 45, 30), c2(3, 20); // hour, min, sec
cout << c1; // add whatever to beautify it
cout << c2;
cout << c1+c2;
c2 = c1+c1;
cout << c2;
Need help please!
//clockType.h
#include<iostream>
using namespace std;
class clockType{
private:
int hour, min, sec;
public:
clockType(int ,int ,int );
clockType operator+(const clockType
&clock);
friend ostream & operator
<< (ostream &out, const clockType &clock);
friend istream & operator >> (istream &in, clockType
&clock);
};
//clockType.cpp
#include"clockType.h"
clockType::clockType(int h=0,int m=0,int s=0){
hour = h;
min = m;
sec=s;
}
clockType clockType::operator+(const clockType &clock){
clockType c;
int s = this->sec+clock.sec;
int m = this->min+clock.min+s/60;
int hr = this->hour+clock.hour+m/60;
c.hour = hr%24;
c.min = m%60;
c.sec = s%60;
return c;
}
ostream & operator << (ostream &out, const clockType
&clock){
out<<clock.hour<<":"<<clock.min<<":"<<clock.sec<<"\n";
return out;
}
istream & operator >> (istream &in, clockType
&clock){
cout<<"Enter hour : ";
in>>clock.hour;
cout<<"Enter minute : ";
in>>clock.min;
cout<<"Enter second : ";
in>>clock.sec;
return in;
}
//lab1main.cpp
#include"clockType.cpp"
int main(){
clockType c1(15, 45, 30), c2(3, 20);
cout << c1;
cout << c2;
cout << c1+c2;
c2 = c1+c1;
cout << c2;
return 0;
}
//sample output