#include <iostream>
using namespace std;
class Date; //对Date类提前引用声明
class Time
{
private:
int hour;
int minute;
int sec;
public:
Time(int h, int m, int s):hour(h),minute(m),sec(s){}
void display(Date &); //Time类的成员函数
};
class Date
{
private:
int month;
int day;
int year;
public:
Date(int m, int d, int y):month(m),day(d),year(y){}
friend void Time::display(Date &);/*友元成员函数:friend函数是Time类中的成员函数*/
};
void Time::display(Date &d)
{
cout << d.month << "月" << d.day << "日" << d.year << "年" << endl;
cout << hour << ":" << minute << ":" << sec << endl;
}
int main()
{
Time t1(12,47,30);
Date d1(6,14,2013);
t1.display(d1);
return 0;
}
C++之友元成员函数
最新推荐文章于 2024-06-20 09:00:44 发布