Object-oriented small exercises

Source: Internet
Author: User

I wrote a diary at night and found that dozens of articles have not been written.
Because the diaries are arranged by the number of days
We need to manually add the days, weather, and weekly articles
Compiled a small program
By the way, let's take a look at object-oriented programming ideas.
This feature allows you to increment by year, month, and day (This feature has been taken into account in a leap year)

[Cpp]
# Define LOCAL
# Include <iostream>
 
Class YEAR // year class
{
Public:
YEAR (): year (0), leap (false ){}
Void setyear (int y) {year = y; leap = leaptest (y );}
Int getyear () {return year ;}
Bool isleap () {return leap ;}
Void add (bool B) {if (B) {++ year; leap = leaptest (year );}}
Private:
Bool leaptest (int y)
{Return (y % 400 = 0) | (y % 100! = 0 & y % 4 = 0 ));}
Int year;
Bool leap;
};
 
Class MONTH // month class
{
Public:
MONTH (): month (0)
{
Int t [12] = {31,28, 31,30, 31,30, 31,31, 30,31, 30,31 };
For (int I = 0; I <12; I ++)
MonthBox [I] = t [I];
}
Void setmonth (int m) {month = m ;}
Int getmonth () {return month ;}
Bool add (bool B) {if (B) {month % = 12; ++ month; return month = 1;} return false ;}
Int contain (bool leap)
{
If (leap & month = 2)
Return 29;
Else
Return monthBox [month-1];
}
Private:
Int monthBox [12];
Int month;
};
 
Class DAY // day class
{
Public:
DAY (): day (0 ){}
Void setday (int d) {day = d ;}
Int getday () {return day ;}
Bool add (int contain) {day % = contain; ++ day; return day = 1 ;}
Private:
Int day;
};
 
Class WEEK // week class
{
Public:
WEEK (): week (0)
{
Std: string t [7] = {"Monday", "Tuesday", "Wednesday", "Thursday", "Friday", "Saturday "};
For (int I = 0; I <7; I ++)
WeekBox [I] = t [I];

}
Void setweek (int w) {week = w; week --;}
Std: string getweek () {return weekBox [week];}
Void add () {(++ week) % = 7 ;}
Private:
Std: string weekBox [7];
Int week;
};
 
Class DATE // date class
{
Public:
DATE (): cnt (0 ){}
Void setdate (int y, int m, int d, int w, int c)
{
Year. setyear (y );
Month. setmonth (m );
Day. setday (d );
Week. setweek (w );
Cnt = c;
}
Void add ()
{
Year. add (month. add (day. add (month. contain (year. isleap ()))));
Week. add ();
Cnt ++;
}
Void printdate ()
{
Std: cout <year. getyear () <"year" <month. getmonth () <"month" <day. getday () <"no." <"clear" <week. getweek () <"<cnt <std: endl;
}
Private:
YEAR year;
MONTH month;
DAY day;
WEEK week;
Int cnt;
};
 
Int main ()
{
# Ifdef LOCAL
Freopen ("input.txt", "r", stdin );
Freopen ("output.txt", "w", stdout );
# Endif
DATE date;
Date. setdate (538 );
Int n;
Std: cin> n;
While (n --)
{
Date. printdate ();
Date. add ();
}
Return 0;
}


From Dreamer Thinker Doer

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.