您的位置:首页 > 其它

YTU-OJ-类中私有成员的访问

2015-05-16 15:33 337 查看


Problem I: 改错题:类中私有成员的访问

Time Limit: 1 Sec Memory Limit: 128 MB

Submit: 252 Solved: 161

[Submit][Status][Web
Board]


Description

/*
改错题:

设计一个日期类和时间类,并编写全局函数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;

}


Input

年 月 日

时 分 秒


Output

年/月/日
时:分:秒


Sample Input

2013  12   23
14   23   50


Sample Output

2013/12/23
14:23:50


HINT

只提交修改过的begin到end部分的代码

/*    
*Copyright (c)2015,烟台大学计算机与控制工程学院    
*All rights reserved.      
*作    者:单昕昕    
*完成日期:2015年5月16日    
*版 本 号:v1.0        
*/ 
/*
改错题:
设计一个日期类和时间类,并编写全局函数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;
    }
    int getyear()const
    {
        return year;
    }
    int getmonth()const
    {
        return month;
    }
    int getday()const
    {
        return day;
    }
private:
    int year;
    int month;
    int day;
};
class Time  //时间类
{
public:
    Time(int h,int m,int s)
    {
        hour=h;
        min=m;
        sec=s;
    }
    int gethour()const
    {
        return hour;
    }
    int getmin()const
    {
        return min;
    }
    int getsec()const
    {
        return sec;
    }
private:
    int hour;
    int min;
    int sec;
};
void display(const Date & d, const Time & t)
{
    cout<<d.getyear()<<"/"<<d.getmonth()<<"/"<<d.getday()<<endl;
    cout<<t.gethour()<<":"<<t.getmin()<<":"<<t.getsec()<<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;
}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: