Description
设计一个日期类和时间类,编写display函数用于显示日期和时间。要求:将Time类声明为Date类的友元类,通过Time类中的display函数引用Date类对象的私有数据,输出年、月、日和时、分、秒。
Input
年、月、日和时、分、秒
Output
年、月、日和时、分、秒
Sample Input
2013 12 2314 23 50
Sample Output
2013/12/2314:23:50
#include<iostream> using namespace std; class Date; class Time { public: Time (int,int,int); void display (Date &); private: int hour,minute,second; }; Time::Time(int a,int b,int c) { hour=a; minute=b; second=c; } class Date { public: Date (int ,int ,int); friend void Time ::display(Date &); private: int year,month,day; }; Date::Date(int a,int b,int c) { year=a; month=b; day=c; } void Time::display(Date &d) { cout<<d.year<<'/'<<d.month<<'/'<<d.day<<endl; cout<<hour<<':'<<minute<<':'<<second<<endl; } 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; }