After learning about classes and objects, in order to make them work, I want to implement a date class with classes and objects, which can implement some calculation methods for dates.
1. Functions
1) Size judgment of date
2) Date change plus minus days
3) Calculate the number of days between two dates
4) Date change plus minus days
2. Implementation Code
I put the declaration of the interface and the definition of the class in the Date1.h file:
#pragma once #include<iostream> using std::cout; using std::endl; class Date { public: inline int GetMonthDay(int year, int month) const //Get specific days per month { static int Montharry[13] = { 0, 31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31 }; if ((month == 2) //Special treatment for February, 29 leap years and 28 non-leap years && ((year % 4 == 0 && year % 100 != 0) || (year % 400 == 0))){ return 29; } return Montharry[month]; } Date(int year,int month,int day) //Custom constructors { if (year >= 1900 && month > 0 && month<13 && day>0 && day <= GetMonthDay(year, month)){ //Determine if the number of days meets the requirements _year = year; _month = month; _day = day; } else cout << "Illegal date!" << endl; } ~Date() { cout << "~Date()" << endl; } void Print() { cout << _year << "-" << _month << "-" << _day << endl; } bool operator>(const Date& d) const; //Judging Date Size bool operator>=(const Date& d) const; bool operator<(const Date& d) const; bool operator<=(const Date& d) const; bool operator==(const Date& d) const; bool operator!=(const Date& d) const; //Add a parameter to distinguish between front and rear ++, or front and rear-- Date& operator++(); // Pre++. Date operator++(int); // Post++. Date operator--(); // Front-- Date operator--(int); // Rear-- Date operator+(int day) const; //Date Plus Minus Days Without Date Change Date operator-(int day) const; Date& operator+=(int day); //Date change plus minus days Date& operator-=(int day); int operator-(const Date&d); //Days between two dates private: int _year; int _month; int _day; };
The implementation of the interface is in the Date1.cpp file:
#include "Date1.h" bool Date::operator>(const Date& d) const { if (_year > d._year){ return true; } else if (_year == d._year){ if (_month > d._month){ return true; } else if (_month == d._month){ if (_day > d._day){ return true; } } } return false; } bool Date::operator<(const Date& d) const { if ((!(*this>d) && !(*this == d))){ return true; } return false; } bool Date::operator!=(const Date& d) const { if (!(*this == d)){ return true; } return false; } bool Date::operator==(const Date& d) const { if (_year == d._year &&_month == d._month&&_day == d._day){ return true; } return false; } bool Date::operator>=(const Date& d) const { if ((*this == d) || (*this > d)){ return true; } return false; } bool Date::operator<=(const Date& d) const { if ((*this == d) || (*this < d)){ return true; } return false; } Date& Date::operator+=(int day) { _day += day; while(_day>GetMonthDay(_year, _month)){ _day -= GetMonthDay(_year, _month); _month++; if (_month == 13){ _year++; _month = 1; } } return *this; } Date& Date::operator++() //Pre++. { *this += 1; return *this; } Date Date::operator++(int) //Post++. { Date res(*this); *this += 1; return res; } Date& Date::operator-=(int day) { _day -= day; while (_day <= 0 ){ _month--; if (_month == 0){ _year--; _month = 12; } _day += GetMonthDay(_year, _month); } return *this; } Date Date::operator--(int) // Rear-- { Date res(*this); *this-=1; return res; } Date Date::operator--() // Front-- { *this -= 1; return *this; } Date Date::operator+(int day) const { Date res(*this); res += day; return res; } Date Date::operator-(int day) const { Date res(*this); res -= day; return res; } int Date::operator-(const Date&d) { if (*this < d){ int cnt = 0; Date res(*this); while (1){ res += 1; cnt++; if (res == d){ return cnt; } } } else if(*this > d){ int cnt = 0; Date ret(d); while (1){ ret += 1; cnt++; if (ret == *this){ return cnt; } } } return 0; }
Except for the last interface, it's better because the implementation >, <=comes out, just need it!All in one go, achieved==,!=Yes, I'm going out, I'm going to implement +=, I'm going to get out all about +, I'm going to implement - =, I'm going to get out all about - The last thought here is to take the big date as the final value and see how many times the small date is going to be added to the big one, which is the number of days between the two dates.
The test function I put in the test.cpp file:
#include<iostream> #include "Date1.h" int main() { Date d1(2019, 4, 30); d1.Print(); Date d2(2018, 5, 13); d2.Print(); Date d3(2020, 1, 31); d3.Print(); Date d4(2019, 6, 6); d4.Print(); cout<<d1.operator>(d2)<<endl; cout << d1.operator==(d2) << endl; cout << d1.operator!=(d2) << endl; cout << d1.operator<(d2) << endl; cout << d1.operator>=(d2) << endl; cout << d1.operator<=(d2) << endl; d1.operator+=(30); //d1 changed d1.Print(); //2019-5-30 d2.operator++(); //d2 changed d2.Print(); //2019-5-14 Date d = d2.operator++(5); //d2m unchanged d.Print(); //2019-5-14 d1.operator-=(30); //d1 changed d1.Print(); //2019-4-30 d1.operator--(); //d1 changed d1.Print(); //2019-4-29 Date s = d1.operator--(5); //d1 unchanged s.Print(); //2019-4-29 d3.operator-=(100); //d3 changed d3.Print(); //2019-10-23 Date q = d4.operator+(100); //d4 unchanged q.Print(); //2019-9-14 int n = d3.operator-(d4); //At this time d3:2019-10-23 d4:2019-6-6 cout << "Two days apart:" << n << "day" << endl; return 0; }
Finally, I would like to say that this date class used to be an interview question for a company, that is, to inspect the mastery of our classes and objects, so it is very important for us to learn the classes and objects well.