将程序中的display函数不放在Time类中,而作为类外的普通函数,然后分别在Time和Date类中将 display声明为友元函数。在主函数中调用display函数, display函数分别引用Time和Date两个类的对象的私有数据,输出年、月、日和时、分、秒。
代码部分:
#include <iostream>
using namespace std;
class Date; //对Date类的提前引用声明
class Time //定义Time类
{
public:
Time(int, int, int);
void display(Date&); //display是成员函数,形参是Date类对象的引用
private:
int hour;
int minute;
int sec;
};
class Date //声明Date类
{
public:
Date(int, int, int);
friend void Time::display(Date&); //声明Time中的 display函数为友元成员函数
private:
int month;
int day;
int year;
};
Time:: Time(int h, int m, int s) //类Time的构造函数
{
hour = h;
minute = m;
sec = s;
}
void Time::display(Date& d) //display的作用是输出年、月、日和时、分、秒
{
cout << d.month << "/" << d.day << "/" << d.year << endl; //引用本类对象中的私有数据
cout << hour << ": " << minute << ": "<<sec<<endl; //引用Date类对象中的私有数据
}
Date:: Date(int m, int d, int y) //类Date的构造函数
{
month = m;
day = d;
year = y;
}
int main()
{
Time t1(05, 55, 55); //定义Time类对象t
Date d1(05, 03, 2002); //定义Date类对象d1
t1.display(d1); //调用t中的 display函数,实参是Date类对象dl
return 0;
}
运行结果: