/*
All rights reserced.
File name: main.cpp
Li Xin
Completion Date: 2016.4.28
Problem Description: Defines the member functions of the following two classes (in order to experience the friend class, this example is not necessarily a good design, merging two classes into a datetime, the date and time are handled better)
*/
#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, 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 defined as the friend class of date Private:int month; int day; int year;}; int main () {Time T1 (23,59,32); Date D1 (2,28,2013); 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;} void time::d isplay (date &d) {cout<<d.year<< "year" <<d.month<< "month" <<d.day<< "Day"; cout<Learning experience: This is not really understand!!
7th Week Item 2-friend class