📄 demo_4_friend_function_4.cpp
字号:
//************************************************
# include <iostream.h>
class Date; //前向引用声明
class Time
{
public:
Time(int,int,int); //构造函数的声明
friend void display(const Date &,const Time &); //友元函数的声明
private:
int hour;
int minute;
int sec;
};
Time::Time(int h,int m,int s) //构造函数的实现
{
hour=h;
minute=m;
sec=s;
}
class Date
{
public:
Date(int,int,int); //构造函数的声明
friend void display(const Date &,const Time &); //友元函数的声明
private:
int month;
int day;
int year;
};
Date::Date(int m,int d,int y) //构造函数的实现
{
month=m;
day=d;
year=y;
}
void display(const Date &d,const Time &t) //友元函数的实现,常对象引用做形参
{
cout<<d.month<<"/"<<d.day<<"/"<<d.year<<endl;
cout<<t.hour<<":"<<t.minute<<":"<<t.sec<<endl;
}
int main()
{
Time t(10,13,56);
Date d(12,25,2004);
display(d,t); //友元函数的调用
return 0;
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -