第7周项目②-----定义下面两个类的成员函数

/*
*Copyright (c) 2016,烟台大学计算机学院
*All rights reserved.
*文件名称:text.cpp
*作者:陈腾
*完成日期:2016年6月7日
*版本号: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;
};
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;
}
int days(int m, int y)
{
    int d[]={0,31,28,31,30,31,30,31,31,30,31,30,31};
    if((y%4==0&&y%100!=0)||(y%400==0)) d[2]=29;
    return d[m];
}
void  Time::add_a_second(Date &d)
{
    sec=sec+1;
    if(sec>=60)
    {
        minute++;
        sec=sec-60;
    }
    if(minute>=60)
        {
            hour=hour+1;
            minute=minute-60;
        }
    if(hour>=24)
    {
        d.day=d.day+1;
        hour=hour-24;
    }


     if(d.day>days(d.month,d.year))
     {d.month=d.month+1;
     d.day=1;
     }
    if(d.month>12)
    {d.year=d.year+1;
     d.month=d.month-12;


}
     }
void Time::display(Date &d)
{
    cout<<d.year<<"年"<<d.month<<"月"<<d.day<<"日"<<endl;
    cout<<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;
    }

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值