已知下列程序,请将display改为普通函数,分别作为Time类和Date的友元函数。在主函数中调用display函数,实现输出和原来程序一样的结果。
时间: 2024-05-09 15:20:44 浏览: 97
```
#include<iostream>
using namespace std;
class Time;
class Date{
int year,month,day;
public:
Date(int y=0,int m=0,int d=0):year(y),month(m),day(d){}
friend void display(Date &d,Time &t);
};
class Time{
int hour,minute,second;
public:
Time(int h=0,int m=0,int s=0):hour(h),minute(m),second(s){}
friend void display(Date &d,Time &t);
};
void display(Date &d,Time &t){
cout<<d.year<<"-"<<d.month<<"-"<<d.day<<" ";
cout<<t.hour<<":"<<t.minute<<":"<<t.second<<endl;
}
int main(){
Date d(2022,10,1);
Time t(12,30,0);
display(d,t);
return 0;
}
```
修改后的程序如下:
```
#include<iostream>
using namespace std;
class Time;
class Date{
int year,month,day;
public:
Date(int y=0,int m=0,int d=0):year(y),month(m),day(d){}
friend void display(Date &d, Time &t);
};
class Time{
int hour,minute,second;
public:
Time(int h=0,int m=0,int s=0):hour(h),minute(m),second(s){}
friend void display(Date &d, Time &t);
};
void display(Date &d, Time &t){
cout<<d.year<<"-"<<d.month<<"-"<<d.day<<" ";
cout<<t.hour<<":"<<t.minute<<":"<<t.second<<endl;
}
int main(){
Date d(2022,10,1);
Time t(12,30,0);
display(d, t);
return 0;
}
```
阅读全文