本文介绍了一个简单的 C++ 程序,该程序定义了 Date 和 Time 两个类,并通过友元函数实现了日期与时间的显示。Date 类包含 day、month 和 year 成员变量,Time 类包含 hour、minute 和 sec 成员变量。
部署运行你感兴趣的模型镜像
#include <iostream>
using namespace std;
class Time;
class Date
{
public:
Date(int ,int ,int);
void display(Time &);
private:
int day;
int month;
int year;
};
Date::Date(int d,int m,int y)
{
int day=d;
int month=m;
int year=y;
}
class Time
{
public:
Time(int,int,int);
friend void Date::display(Time &);
private:
int hour;
int minute;
int sec;
};
Time::Time(int h,int m,int s)
{
hour=h;
minute=m;
sec=s;
}
void Date::display(Time &t)
{
cout<<day<<" "<<month<<" "<<year<<endl;
cout<<t.hour<<" "<<t.minute<<" "<<t.sec<<endl;
}
int main()
{
Time t(10,13,56);
Date d(12,25,2004);
d.display(t);
return 0;
}