zl程序教程

您现在的位置是:首页 >  工具

当前栏目

友元成员函数的简单应用

应用 函数 简单 成员 友元
2023-09-14 09:12:06 时间
 1 #include <iostream>
 2 
 3 /* run this program using the console pauser or add your own getch, system("pause") or input loop */
 4 using namespace std;
 5 class Date;
 6 class Time
 7 {
 8     public:
 9         Time(int,int,int);
10         void display(Date &);
11     private:
12         int hour;
13         int minute;
14         int sec;
15 };
16 
17 class Date
18 {
19     public:
20         Date(int,int,int);
21         friend void Time::display(Date &);
22     private:
23         int month;
24         int day;
25         int year;
26 };
27 
28 Time::Time(int h,int m,int s)
29 {
30     hour=h;
31     minute=m;
32     sec=s;
33 }
34 
35 void Time::display(Date &d)
36 {
37     cout<<d.month<<"/"<<d.day<<"/"<<d.year<<endl;
38     cout<<hour<<":"<<minute<<":"<<sec<<endl;
39 }
40 
41 Date::Date(int m,int d,int y)
42 {
43     month=m;
44     day=d;
45     year=y;
46 }
47 
48 int main(int argc, char** argv) {
49     Time t1(10,13,56);
50     Date d1(12,25,2004);
51     t1.display(d1);    
52     return 0;
53 }