第六周项目5 友元类

来源:互联网 发布:阿里域名 编辑:程序博客网 时间:2024/06/05 07:27
/**Copyright (c)2015,烟台大学计算机与控制工程学院*All rights reserved.*文件名称:score.cpp*作    者:惠睿*完成日期:2015年4月10日*版 本 号:v1.0**问题描述:定义两个类的成员函数,为体验友元类,实际上本类并不一定是一个好的设计,将两个类合并成一个 DateTime 类,日期,时间会处理得更好。*程序输入:无输入。*程序输出:输出时间。*/#include <iostream>using namespace std;class Date;class Time{public:    Time(int,int,int);    void add_a_second(Date &);    void display(Date &);private:    int hour;    int minute;    int sec;};class Date{public:    Date(int,int,int);    friend class Time;private:    int month;    int day;    int year;};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;}Time::Time(int h,int m,int s){    hour=h;    minute=m;    sec=s;}void Time::add_a_second(Date &t){    int a[13]= {0,31,28,31,30,31,30,31,31,30,31,30,31};    sec+=1;    if(sec>59)    {        sec=0;        minute+=1;    }    if(minute>59)    {        minute=0;        hour+=1;    }    if(hour>23)    {        hour=0;        t.day+=1;    }    if(t.day>a[t.month])    {        if(t.month==2)        {            if((t.year%4==0 && t.year%100!=0) || (t.year%400==0))                ;            else            {                t.month+=1;                t.day=1;            }        }        else        {            t.month+=1;            if(t.month>12)            {                t.year+=1;                t.month=1;            }            t.day=1;        }    }}void Time::display(Date &t){    cout<<t.year<<"年"<<t.month<<"月"<<t.day<<"日  "<<hour<<":"<<minute<<":"<<sec<<endl;}Date::Date(int m,int d,int y){    year=y;    month=m;    day=d;}


运行结果:

 

0 0
原创粉丝点击