上机内容:友元类的练习
上机目的;掌握友元类的使用
/*
* 文件名称:友元类
* 作 者:甘立文
* 完成日期:2016年4月12日
* 版 本 号:v1.0
* 对任务及求解方法的描述部分:
* 输入描述:
* 问题描述:
* 程序输出:
* 问题分析:
* 算法设计:
*/
#include <iostream>
using namespace std;
class Date
{
public:
Date(int=0,int=0,int=0);
friend class Time;
private:
int month;
int day;
int year;
};
Date::Date(int m,int d,int y)
{
month=m;day=d;year=y;
}
class Time
{
public:
Time(int=0,int=0,int=0);
void add_a_second(Date &p1);
void display(Date &p2);
private:
int hour;
int minute;
int sec;
};
Time::Time(int h,int m,int s)
{
hour=h;minute=m;sec=s;
}
void Time::add_a_second(Date &p1)
{
sec+=1;
minute+=sec/60;
sec=sec%60;
hour+=minute/60;
minute=minute%60;
p1.day+=hour/24;
hour=hour%24;
if(p1.day>31)
{
p1.day=1;
p1.month+=1;
if(p1.month>12)
{
p1.month=1;
p1.year+=1;
}
else p1.year=p1.year;
}
else
{
p1.day=p1.day;
p1.month=p1.month;
p1.year=p1.year;
}
}
void Time::display(Date &p2)
{
cout<<p2.year<<"年"<<p2.month<<"月" <<p2.day<<"日"<<hour<<":"<<minute<<":"<<sec<<endl;
}
int main( )
{
Time t1(23,59,32);
Date d1(12,31,2013);
for(int i=0; i<=100; i++)
{
t1.add_a_second(d1);
t1.display(d1);
}
return 0;
}
心得体会:友元类需要分清楚主次
知识点总结: