Week six item five-friend class

Source: Internet
Author: User

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

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.