7-4-友元函数
2014-04-13 22:41
211 查看
01./* 02.* 程序的版权和版本声明部分: 03.* Copyright (c) 2011, 烟台大学计算机学院 04.* All rights reserved. 05.* 文件名称:test.cpp 06.* 作 者:刘芳 07.* 完成日期:2014 年04 月13 日 08.* 版 本 号:v1.0 09.* 对任务及求解方法的描述部分: 10.* 输入描述:无 11.* 问题描述: 12.* 程序输出: 13.* 问题分析:略 14.* 算法设计:略 15.*/
#include<iostream> using namespace std; class Date; //对Date类的提前引用声明 class Time { public: Time(int,int,int); void add_a_second(Date &); //增加1秒,1秒后可能会到了下一天,乃到下一月、下一年 void display(Date &); //显示时间,格式:月/日/年 时:分:秒 private: int hour; int minute; int sec; }; class Date { public: Date(int,int,int); friend class Time; //Time为Date的友元类 private: int month; int day; int year; }; 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; } //下面定义两个类中的成员函数,要求不得再增加成员函数 //注意体会在Time的成员函数中可以调用Date类的私有数据成员 Time::Time(int h,int m,int s) { hour=h; minute=m; sec=s; } Date::Date(int m,int d,int y) { month=m; day=d; year=y; } void Time::display(Date &t) { cout<<"增加一秒后的时间:"<<endl; cout<<t.month<<"/"<<t.day<<"/"<<t.year<<endl; cout<<hour<<":"<<minute<<":"<<sec<<endl; } void Time::add_a_second(Date &t) { if(sec<59) sec++; else if(sec==59&&minute<59) { sec=0; minute++; } else if(sec==59&&minute==59&&hour<23) { sec=0; minute=0; hour++; } else if(sec==59&&minute==59&&hour==23); { if((t.year%4==0&&t.year%100!=0)||t.year%400==0) { if(t.month==2&&t.day<29) { t.day++; } else if(t.month==2&&t.day==29) { t.month++; t.day=1; } else if((t.month==1||t.month==3||t.month==5|| t.month==7||t.month==8||t.month==10)&&t.day==31) { t.day=1; t.month++; } else if((t.month==4||t.month==6||t.month==9|| t.month==11)&&t.day<30) { t.day++; } else if((t.month==1||t.month==3||t.month==5|| t.month==7||t.month==8||t.month==10||t.month==12)&&t.day==30) { t.day=1; t.month++; } else if(t.month==12&&t.day==31) { t.year++; t.month=1; t.day=1; } } else { { if(t.month==2&&t.day<28) { t.day++; } else if(t.month==2&&t.day==28) { t.month++; t.day=1; if(sec<59) sec++; else if(sec==59&&minute<59) { sec=0; minute++; } else if(sec==59&&minute==59&&hour<23) { sec=0; minute=0; hour++; } } else if(t.month==2&&t.day<29) { t.day++; if(sec<59) sec++; else if(sec==59&&minute<59) { sec=0; minute++; } else if(sec==59&&minute==59&&hour<23) { sec=0; minute=0; hour++; } } else if(t.month==2&&t.day==29) { t.day=1; t.month++; hour=0; } else if((t.month==1||t.month==3||t.month==5|| t.month==7||t.month==8||t.month==10||t.month==12)&&t.day<31) { t.day++; hour=0; minute=0; sec=0; } else if((t.month==1||t.month==3||t.month==5|| t.month==7||t.month==8||t.month==10)&&t.day==31) { t.day=1; t.month++; hour=0; minute=0; sec=0; } else if((t.month==4||t.month==6||t.month==9|| t.month==11)&&t.day<30) { t.day++; hour=0; minute=0; sec=0; } else if((t.month==1||t.month==3||t.month==5|| t.month==7||t.month==8||t.month==10||t.month==12)&&t.day==30) { t.day=1; t.month++; hour=0; minute=0; sec=0; } else if(t.month==12&&t.day==31) { t.year++; t.month=1; t.day=1; hour=0; minute=0; sec=0; } } } }
相关文章推荐
- 第七周上机实践项目——友元类
- 多线程的等待唤醒机制
- 深入探讨 Java 类加载器
- java文件操作类实例
- 黑马程序员_OC中的分类(Category)的使用
- 安装opencms时遇到问题及解决方法
- CAsyncSocket编程 MFC
- 关于人生的思考
- jvm
- ubuntu12.04中如何设定中文输入法
- Leetcode | Merge Intervals
- JQuery页面回车事件
- 湖南科技大学2014 ACM 校赛
- 3Sum Closest
- tomcat类加载器
- 组合索引leaf 数据存储
- PB实现数据窗口的多表更新
- SecureCRT 上传和下载文件
- C和C++中return的用法
- MEF核心笔记(6)让 MEF 拥抱 AOP