简答题 C++
题号 #2657078

要求:
      将课堂实验中的time类声明为date类的友元类,通过time类中的display函数引用date类对象的私有数据,输出年、月、日和时、分、秒。

进入在线模考

参考答案

暂无

答案解析

#include <iostream>
using namespace std;
class date;
class time
{
public:
    time(int,int,int);
    void display(date &);
private:
    int hour;
    int minute;
    int second;
};
class date
{
friend class time;
public:
    date(int,int,int);
private:
    int year;
    int month;
    int day;
};
time::time(int h,int min,int se)
{
    hour=h;
    minute=min;
    second=se;
}
date::date(int y,int mon,int d)
{
    year=y;
    month=mon;
    day=d;
}
void time::display(date &da)
{
    cout<<da.year<<"-"<<da.month<<"-"<<da.day<<endl;
    cout<<hour<<":"<<minute<<":"<<second<<endl;
}
int main()
{
    time t1(12,23,34);
    date d1(2010,11,7);
    t1.display(d1);
    return 0;
}

你可能感兴趣的试题

找试题 · 找答案 · 找资料 在线模考