设计类Time(用来处理时、分、秒)和类Data(用来处理年、月、日)

设计类Time(用来处理时、分、秒)和类Data(用来处理年、月、日),然后分别在两个类中声明display函数为其友员函数。在主调函数中调用display函数,display函数分别引用两个类的对象的私有数据,输出年、月、日、时、分、秒。(请注意形参的合理定义)

你题目的解答代码如下:

#include <iostream>
using namespace std;
 
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 t1(10, 13, 56);
    Date d1(12, 25, 2004);
    display(d1, t1);
    return 0;
}

如有帮助,请点击我的回答下方的【采纳该答案】按钮帮忙采纳下,谢谢!

img

您好,我是有问必答小助手,您的问题已经有小伙伴帮您解答,感谢您对有问必答的支持与关注!
PS:问答VIP年卡 【限时加赠:IT技术图书免费领】,了解详情>>> https://vip.csdn.net/askvip?utm_source=1146287632