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, with a better date and time).
/** Copyright (c) 2015, Yantai University School of Computer * All right reserved.* Zhao Song * file: demo.cpp* finish: April 22, 2015 * version number: v1.0*/#include <iostream > #include <cmath>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 the friend class of date Private:int month; int day; int year;};/ /The following defines a member function in two classes, requiring no further addition of member functions//Notice the private data member int day (int,int) of the date class can be called 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;} void Time::add_a_second (Date &d) {sec++; if (sec>60) {minute++; Sec=1; } if (minute>60) {hour++; minute=1; } if (hour>24) {d.day++; Hour=1; } if (D.day>day (d.month,d.year)) {d.month++; D.day=1; } if (d.month>12) {d.year++; d.month=1; }}void Time:: Display (Date &d) {cout<<d.month<< "/" <<d.day<< "/" <<d.year<< "" <
Operation Result:
Week six item five-friend class