Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <iostream>
- #include <string>
- #include <vector>
- #include <stdexcept>
- using namespace std;
- bool leapYear(int year) {
- return (year % 400 == 0 || (year % 4 == 0 && year % 100 != 0));
- }
- class Date {
- private:
- static constexpr int dayMonths[12] = { 31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31 };
- int year, month, day;
- public:
- Date() :year(-1), month(-1), day(-1) {}
- Date(int year, int month, int day) :year(year), month(month), day(day) {}
- friend Date& operator ++(Date& d);
- friend Date operator ++(Date& d, int);
- friend ostream& operator <<(ostream& out, Date& d);
- };
- Date& operator ++(Date& d) {
- d.day++;
- if (d.day == d.dayMonths[d.month - 1] + 1 + (d.month == 2 && leapYear(d.year))) {
- d.day = 1;
- d.month++;
- if (d.month == 13) {
- d.month = 1;
- d.year++;
- }
- }
- return d;
- }
- Date operator ++(Date& d, int) {
- Date old = d;
- ++d;
- return old;
- }
- ostream& operator <<(ostream& out, Date& d) {
- out << d.day << "." << d.month << "." << d.year << endl;
- return out;
- }
- int main() {
- Date d(2019, 12, 30);
- cout << d;
- Date d1 = d++;
- cout << d1 << d;
- ++d;
- cout << d;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement