一、问题及代码。
#include<iostream>
using namespace std;
class Date;
class Time
{
public:
Time(int,int,int);
void add_a_second(Date &);
void display(Date &);
friend class Date;
private:
int hour;
int minute;
int sec;
int month;
int day;
int year;
};
class Date
{
public:
Date(int,int,int);
friend class Time;
private:
int month;
int day;
int year;
};
int main( )
{
Time t1(23,59,32);
Date d1(12,31,2013);
for(int i=0; i<=100; i++)
{
t1.add_a_second(d1);
t1.display(d1);
}
return 0;
}
Date::Date(int m,int d,int y)
{
month=m;
day=d;
year=y;
}
Time::Time(int h,int a,int s)
{
hour=h;
minute=a;
sec=s;
}
void Time:: add_a_second(Date &d)
{
if(sec+1>=60&&minute>=59&&hour>=23&&day>=30&&month>=12)
{
sec=sec%60;
minute=(minute+1)%60;
hour=(hour+1)%24;
day=(day+1)%30;
month=(month+1)%12;
year=year+1;
}
else
{
sec=(sec+1)%60;
minute=(minute+1)%60;
hour=(hour+1)%24;
day=day;
month=month;
year=year;
}
}
void Time::display(Date &d)
{
cout<<d.year<<"/"<<d.month<<"/"<<d.day<<" ";
cout<<hour<<":"<<minute<<":"<<sec<<" "<<endl;
}
二、运行结果。
三、心得体会。
结果还是不对,很多的提示都不知道是什么错误,对于一些概念性的东西还是不能很好的理解,代码中有些问题分析也不全面,但是不知道怎样完善。
四、知识点总结。
1、对友函数的理解和应用。
2、对问题的多方面思考。
3、对以前的知识点的运用。