YTU-OJ-类中私有成员的访问
2015-05-16 15:33
337 查看
Problem I: 改错题:类中私有成员的访问
Time Limit: 1 Sec Memory Limit: 128 MBSubmit: 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; }
相关文章推荐
- [YTU]_2639 ( 改错题:类中私有成员的访问)
- YTU 2639: 改错题:类中私有成员的访问
- YTU-OJ-Problem J: B3 指向基类的指针访问派生类的成员函数
- OJ第二批——Problem I:改错题:类中私有成员的访问
- C# 反射机制(reflection)实现对私有成员的访问
- 测试在类的内部是否可以访问对象的私有成员
- .NET程序内,访问私有或者保护成员的技巧
- C++中巧用#define访问类的私有成员
- C#属性详解(c# 通过公共属性访问私有成员 优点)
- 关于类成员函数中访问同类对象的私有成员
- VC6.0中友元函数无法访问类私有成员的解决办法
- 通过操作地址的方法来访问C++类对象中私有成员变量
- 如何访问私有成员变量和函数 (修正版)
- 3第一周课后练习·阅读计划(2)-使用指针来访问私有数据成员
- YTU 2634: E3 继承了,成员函数却不可访问
- 友员函数访问类私有成员例子
- JavaSE第六十四讲:使用反射机制调用对象的私有方法、访问对象的私有成员变量
- 在重载运算符时,类的对象可以直接访问私有成员解惑
- 第04周:项目0:私有数据成员的访问方式
- C++中类的访问权限针对的是类而不是对象!(为什么类中的函数可以访问对象的私有成员?)