7th Week Item 2-friend class

Source: Internet
Author: User

/*
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

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.