Problem H: C++ 习题 输出日期时间--友元类
Time Limit: 1 Sec Memory Limit: 128 MBSubmit: 1011 Solved: 533
[ Submit][ Status][ Web Board]
Description
设计一个日期类和时间类,编写display函数用于显示日期和时间。要求:将Time类声明为Date类的友元类,通过Time类中的display函数引用Date类对象的私有数据,输出年、月、日和时、分、秒。
Input
年、月、日和时、分、秒
Output
年、月、日和时、分、秒
Sample Input
2013 12 23
14 23 50
Sample Output
2013/12/23
14:23:50
HINT
#include <iostream>
using namespace std;
class Date;
class Time
{
private:
int hour;
int minute;
int second;
public:
Time(int h,int m,int s):hour(h),minute(m),second(s){}
void display(Date &d);
};
class Date
{
private:
int year;
int month;
int day;
public:
friend class Time;
Date(int y,int m,int d):year(y),month(m),day(d){}
};
void Time::display(Date &d)
{
cout<<d.year<<"/"<<d.month<<"/"<<d.day<<endl;
cout<<hour<<":"<<minute<<":"<<second;
}
int main()
{
int year,month,day;
cin>>year>>month>>day;
Date d1(year,month,day);
int hour,minute,second;
cin>>hour>>minute>>second;
Time t1(hour,minute,second);
t1.display(d1);
return 0;
}