您的位置:首页 > 其它

第七周项目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="" />
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: