您的位置:首页 > 其它

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;

}

}

}
}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: