![]() |
#2
lianjiecuowu2011-06-09 13:06
#include<iostream>
using namespace std; class Time { private: int hours,minutes,seconds; public: Time(void){}; Time(int h,int m,int s); Time(Time &t); ~Time(){cout<<"the end"<<endl;} void settime(); int gettime(); void add(Time&t1,Time&t2); }; Time::Time(int h,int m,int s) { hours=h;minutes=m;seconds=s; cout<<"现在的时间为:"<<hours<<":"<<minutes<<":"<<seconds<<endl; } Time::Time(Time &t) { hours=t.hours;minutes=t.minutes;seconds=t.seconds; cout<<"现在的时间为:"<<hours<<":"<<minutes<<":"<<seconds<<endl; } void Time::settime() { int h,m,s; cin>>h>>m>>s; hours=h; minutes=m; seconds=s; } int Time::gettime() { cout<<"现在的时间为:"<<hours<<":"<<minutes<<":"<<seconds<<endl; return 0; } void Time::add(Time&t1,Time&t2) { Time total; const int perminutes=60; const int perhours=60; total.seconds=(t1.seconds+t2.seconds)%perminutes; total.minutes=(t1.minutes+t2.minutes+(t1.seconds+t2.seconds)/perminutes)%perhours; total.hours=(t1.hours+t2.hours+(t1.minutes+t2.minutes+(t1.seconds+t2.seconds)/perminutes)/perhours); cout<<"现在的时间为:"<<total.hours<<":"<<total.minutes<<":"<<total.seconds<<endl; } int main() { Time T; Time a(5,5,23); cout<<"请设置当前的时间:"<<endl; a.settime(); a.gettime(); cout<<endl<<endl; Time b(6,4,34); cout<<endl<<endl; T.add(a,b); system("pause"); return 0; } ![]() ![]() |
using namespace std;
class Time
{
private:
int hours,minutes,seconds;
public:
Time(void){};
Time(int h,int m,int s);
Time(Time &t);
~Time(){cout<<"the end"<<endl;}
void settime();
int gettime();
void add(Time&t1,Time&t2);
};
Time::Time(int h,int m,int s)
{
hours=h;minutes=m;seconds=s;
cout<<"现在的时间为:"<<hours<<":"<<minutes<<":"<<seconds<<endl;
}
Time::Time(Time &t)
{
hours=t.hours;minutes=t.minutes;seconds=t.seconds;
cout<<"现在的时间为:"<<hours<<":"<<minutes<<":"<<seconds<<endl;
}
void Time::settime()
{
int h,m,s;
cin>>h>>m>>s;
hours=h;
minutes=m;
seconds=s;
}
int Time::gettime()
{
cout<<"现在的时间为:"<<hours<<":"<<minutes<<":"<<seconds<<endl;
return 0;
}
void Time::add(Time&t1,Time&t2)
{
Time total;
const int perminutes=60;
const int perhours=60;
total.seconds=(t1.seconds+t2.seconds)%perminutes;
total.minutes=(t1.minutes+t2.minutes+(t1.seconds+t2.seconds)/perminutes)%perhours;
total.hours=(t1.hours+t2.hours+(t1.minutes+t2.minutes+(t1.seconds+t2.seconds)/perminutes)/perhours);
cout<<"现在的时间为:"<<total.hours<<":"<<total.minutes<<":"<<total.seconds<<endl;
}
int main()
{
Time a(5,5,23);
cout<<"请设置当前的时间:"<<endl;
a.settime();
a.gettime();
cout<<endl<<endl;
Time b(6,4,34);
add(a,b); //error C2065: 'add' : undeclared identifier

system("pause");
return 0;
}