第七周上机实践项目——项目2-友元类

来源:互联网 发布:aes解密算法 编辑:程序博客网 时间:2024/04/30 08:29
/* *Copyright (c)2016,烟台大学计算机与控制工程学院 *All rights reserved. *文件名称:main.cpp *作    者:郭永恒 *完成日期:2016年4月11日 *版 本 号:v1.0 * *问题描述:定义类的成员函数,不得再增加成员函数 */#include <iostream>using namespace std;int months[13] = {0,31,28,31,30,31,30,31,31,30,31,30,31};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;};Date::Date(int m,int d,int y):month(m),day(d),year(y) {}Time::Time(int h = 0,int m = 0,int s = 0):hour(h),minute(m),sec(s) {}void Time::add_a_second(Date &TD){    sec += 1;    if(sec > 59)    {        minute += 1;//每次只加一秒,这里可以放心的加一分钟        sec = 0;//每次只加一秒,这里可以放心的变成0    }    if(minute > 59)    {        hour += 1;        minute = 0;    }    if(hour > 23)    {        TD.day += 1;        hour = 0;    }    if(TD.day > months[TD.month])    {        TD.month += 1;        TD.day = 1;    }    if(TD.month > 12)    {        TD.year += 1;        TD.month = 1;    }}void Time::display(Date &TD){    cout << TD.year << "/" << TD.month << "/" << TD.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
原创粉丝点击