第七周项目2-友元类
2016-04-20 19:54
423 查看
/* *Copyright(c) 2016,烟台大学计算机与控制工程学院 *All rights reserved. *文件名称:my.cpp *作 者:张瀚文 *完成日期:2016年4月20日 *版 本 号:v1.0 * *问题描述:1.为示例 2.模仿上面,完成点类中距离的任务。分别用 成员函数,友元函数和一般函数求2点间的距离。 *输入描述: *程序输出: */ #include<iostream> using namespace std; class Date; class Time { public: Time(int x,int y ,int z):hour(x),minute(y),sec(z){}; void add_a_second(Date &);//增加1秒,1秒后可能回到了下一天乃到下一月下一年; void display(Date &);//显示时间,格式:月/日/年 时:分:秒 private: int hour; int minute; int sec; }; class Date { public: Date(int x, int y ,int z):month(x),day(y),year(z){}; friend class Time;//Time 为Date类的友元类 private: int month; int day; int year; }; void Time::add_a_second(Date &t) { sec++; if(sec>59) { minute++; sec=0; if(minute>59) { hour++; minute=0; if(hour>23) { hour=0; t.day++; if(t.month==4||t.month==9||t.month==6||t.month==10) { if (t.day>30) { t.day=1; t.month++; } } else if(t.month==2) { if(t.year%4==0&&t.year%100!=0||t.year%400==0) { if(t.day>29) { t.day=1; t.month++; } } else { if(t.day>28) { t.day=1; t.month++; } } } else if (t.day>31) { t.day=1; t.month++; } if(t.month>12) { t.month=1; t.year++; } } } } } void Time::display(Date &t) { cout<<t.month<<' '<<t.day<<' '<<t.year<<' '<<hour<<":"<<minute<<":"<<sec<<endl; } int main() { Time t1(23,59,32); Date d1(12,31,2013);//测试时,再试试Date d1(2,28,2013) for(int i=0;i<=100;i++) { t1.add_a_second(d1); t1.display(d1); } return 0; }
<img src="https://img-blog.csdn.net/20160420195414171" alt="" /><img src="https://img-blog.csdn.net/20160420195427296" alt="" />
相关文章推荐
- 【bzoj4542】[Hnoi2016]大数 莫队
- java序列化(Serializable)的作用和反序列化
- Leetcode——binary-tree-maximum-path-sum
- 如何利用代码标注 MATLAB 图像上的某些点
- 操作系统开发系列—12.e.Makefile
- MapKit框架学习MKCircle 和MKCircleView
- 关于Java静态代码块、初始化块、构造函数的调用顺寻问题?
- Xcode在调试时查看到变量都是nil的问题
- 利用PHP的GD2图像函数 + mysql实现的一个简单的投票系统
- HDU-1020-Encoding,题意不清,其实很水~~
- 第八周
- zzuli 1877 蛤玮打扫教室
- 美团外卖app可行性分析
- 【Unity】6.8 Quaternion类(四元数)
- leecode Nim Game
- 数据总线和地址总线
- 在一个没有大神的公司如何成长
- 团队开发个人总结02
- 团队站立会议02
- JSP如何利用session在关闭浏览器时,自动清除缓存