2436: C++ 习题 输出日期时间--友元类
时间限制: 1 Sec 内存限制: 128 MB
提交: 1243 解决: 690
题目描述
设计一个日期类和时间类,编写display函数用于显示日期和时间。要求:将Time类声明为Date类的友元类,通过Time类中的display函数引用Date类对象的私有数据,输出年、月、日和时、分、秒。
输入
年、月、日和时、分、秒
输出
年、月、日和时、分、秒
样例输入
2013 12 23
14 23 50
样例输出
2013/12/23
14:23:50
提示
主函数已给定如下,提交时不需要包含下述主函数
/* C++代码 */
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;
}
迷失在幽谷中的鸟儿,独自飞翔在这偌大的天地间,却不知自己该飞往何方……
#include <iostream>
using namespace std;
class Date
{
public:
Date(int y,int m,int d);
int year,month,day;
};
class Time
{
public:
Time(int y,int m,int d);
void display(Date);
int hour,minute,second;
};
void Time::display(Date d)
{
cout<<d.year<<"/"<<d.month<<"/"<<d.day<<endl;
cout<<hour<<":"<<minute<<":"<<second<<endl;
}
Date::Date(int y,int m,int d)
{
year=y;
month=m;
day=d;
}
Time::Time(int y,int m,int d)
{
hour=y;
minute=m;
second=d;
}
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;
}
#include <iostream>
using namespace std;
class Date
{
public:
Date(int y,int m,int d);
int year,month,day;
};
class Time
{
public:
Time(int y,int m,int d);
void display(Date);
int hour,minute,second;
};
void Time::display(Date d)
{
cout<<d.year<<"/"<<d.month<<"/"<<d.day<<endl;
cout<<hour<<":"<<minute<<":"<<second<<endl;
}
Date::Date(int y,int m,int d)
{
year=y;
month=m;
day=d;
}
Time::Time(int y,int m,int d)
{
hour=y;
minute=m;
second=d;
}
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;
}