Item five--friend class--week six

Source: Internet
Author: User
Tags time and seconds

Define member functions for the following two classes (in order to experience a friend class, in fact this example is not necessarily a good design, merging two classes into a datetime, the date and time are handled better)

Class Date; Advance reference to the Date class declares class Time{public: Time    (int,int,int);    void Add_a_second (Date &);  Add 1 seconds, 1 seconds after the next day, to the next month, the next year,    void display (Date &);  Display time, Format: Month/day/year: minutes: Seconds private:    int hour;    int minute;    int sec;}; Class Date{public:    Date (int,int,int);    Friend class time; Time is a friend of date, private:    int month;    int day;    int year;}; int main () {time    T1 (23,59,32);    Date D1 (12,31,2013);   When testing, try again how the date D1 (2,28,2013) will be for    (int i=0; i<=100; i++)    {        t1.add_a_second (D1);        T1.display (D1);    }    return 0;} The following defines a member function in two classes, requiring no further addition of member functions//Notice the private data members of the date class can be called in the member function of time   
Reference Answer:

/* Copyright (c) 2014, Yantai University School of Computer * All rights reserved. * File name: Test.cpp * Chen Dani * Completion Date: January 27, 2015 * Version number: v1.0 */#include <iostream>using namespace Std;class date;    Advance reference to the Date class declares class Time{public:time (Int,int,int);  void Add_a_second (Date &);  Add 1 seconds, 1 seconds after the next day, to the next month, the next year, void Display (Date &);    Display time Private:int hour;    int minute; int sec;};    Class Date{public:date (Int,int,int); Friend class time;    Time is the friend class of date Private:int month;    int day; int year;};    int main () {Time T1 (23,59,32);   Date D1 (2,28,2013);        When testing, try again how the date D1 (2,28,2013) will be for (int i=0; i<=100; i++) {t1.add_a_second (D1);    T1.display (D1); } return 0;}    The following defines a member function in two classes, requiring no further addition of member functions//Notice that you can call the private data member of the date class in the member function of time time::time (int h,int m,int s) {hour=h;    Minute=m; Sec=s;}    Date::D ate (int m,int d,int y) {month=m;    Day=d; Year=y;}   int days (int m,int y);     The days function returns y years M months of the day void Time::add_a_second (Date &d) {if (++sec>=60) {   minute++,sec-=60;        if (++minute>=60) hour++,minute-=60;        if (++hour>=24) d.day++,hour-=24;        if (D.day>days (d.month,d.year)) D.month++,d.day=1;    if (d.month>12) d.year++,d.month-=12;    }}int days (int m,int y) The//days function returns the number of years of the y year m month {int d[]= {31,28,31,30,31,30,31,31,30,31,30,31}; if ((y%100==0&&y%4!=0) | | |        (y%400==0))    d[1]=29; return d[m-1];}    void time::d isplay (date &d) {cout<<d.year<< "year" <<d.month<< "month" <<d.day<< "Day"; cout<


Experience: Note that this is only added for 1 seconds, instead of adding time and seconds. The premise is to add 1 seconds to brace. Friends of the class is very useful!

Come on, let's go!

Item five--friend class--week six

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.