YTU 2639: 改錯題:類中私有成員的訪問

2639: 改錯題:類中私有成員的訪問

時間限制: 1 Sec   內存限制: 128 MB
提交: 431   解決: 297

題目描述

/*
改錯題:
設計一個日期類和時間類,並編寫全局函數display用於顯示日期和時間。要求:display函數做爲類外的普通函數,而不是成員函數
在主函數中調用display函數,display函數分別引用Time和Date兩個類的對象的私有數據,輸出年、月、日和時、分、秒。
下面的程序中,在begin到end部分存在語法錯誤。請改正錯誤,使程序按下面輸入輸出的規定運行。
注意:只提交修改過的begin到end部分的代碼。
*/
#include <iostream>
using namespace std;
//將程序須要的其餘成份寫在下面,只提交修改後的begin到end部分的代碼
//******************** begin ********************
class Time;  //類的提早聲明
class Date   //日期類
{
public:  
Date(int y,int m,int d)
       {
               year=y;
month=m;
day=d;
       }
 private:
int year;
int month;
int day;
}; 
class Time  //時間類
{  
public:  
   Time(int h,int m,int s)
   {
        hour=h;
min=m;
sec=s;
   }
 private:
int hour;
int min;
int sec;
}; 
void display(const Date & d, const Time & t)
{
    cout<<d.year<<"/"<<d.month<<"/"<<d.day<<endl;
cout<<t.hour<<":"<<t.min<<":"<<t.sec<<endl;   
}
//********************* end ********************
int main()
{
    void display(const Date &,const Time &);
    int year,month,day;
    cin>>year>>month>>day;
    Date d1(year,month,day);
    int hour,minute,second;
    cin>>hour>>minute>>second;
    Time t1(hour,minute,second);
    display(d1,t1);
    return 0;
}

輸入

年      月       日ios

時      分       秒函數

輸出

年/月/日spa

時:分:秒

樣例輸入

2013  12   23
14   23   50

樣例輸出

2013/12/23
14:23:50

提示

只提交修改過的begin到end部分的代碼設計


迷失在幽谷中的鳥兒,獨自飛翔在這偌大的天地間,殊不知本身該飛往何方……

#include <iostream>
using namespace std;
class Time;  //類的提早聲明
class Date   //日期類
{
public:
    Date(int y,int m,int d)
    {
        year=y;
        month=m;
        day=d;
    }
    int year;
    int month;
    int day;
};
class Time  //時間類
{
public:
    Time(int h,int m,int s)
    {
        hour=h;
        min=m;
        sec=s;
    }
    int hour;
    int min;
    int sec;
};
void display(const Date & d, const Time & t)
{
    cout<<d.year<<"/"<<d.month<<"/"<<d.day<<endl;
    cout<<t.hour<<":"<<t.min<<":"<<t.sec<<endl;
}
int main()
{
    void display(const Date &,const Time &);
    int year,month,day;
    cin>>year>>month>>day;
    Date d1(year,month,day);
    int hour,minute,second;
    cin>>hour>>minute>>second;
    Time t1(hour,minute,second);
    display(d1,t1);
    return 0;
}
相關文章
相關標籤/搜索