C++ Elementary Date Class Implementation (Part 1)

Table of contents

1. Preparation

1.1 Get the number of days in each month

1.2 Get the number of days per year

1.3 Constructor, destructor and copy constructor

2. Implementation of +,-,+=,-= for dates and days

2.1+= operator overloading

2.2+ operator implementation

2.3-Implementation of operator

2.4-Implementation of operators

3. Implementation of ++, --

3.1 Implementation of pre-++ and post-++

3.2 Implementation of pre-- and post--

4. Preview of next issue


Preface: In each issue of the C++ Beginner Series, bloggers will use simple and plain language to share the corresponding knowledge with everyone, striving to be understood by everyone. The C++ Beginner Series will continue to be updated, and will be updated from time to time during the school year. But there will always be more

1. Preparation

1.1 Get the number of days in each month

The number of days in a month is a very important thing. February is the most special. In leap years, it is 29, and in other years it is 28. We can use an array to operate, and the creation of this array is also particular. Create A length of 13 is best.

Because the subscript of the array starts from 0, and the date starts from 1, if we want the month to correspond to the subscript of the array, we have to give up a space, so the length we created is 13. After creating the array Just set the value of the first subscript to 0, start with the value of the next subscript to give the number of days corresponding to the month, and then deal with the leap year situation separately outside, and finally return the number of days corresponding to the month and you are done.

	int GetMonthDay(int year, int month)
	{
		int arr[13] = { 0,31,28,31,30,31,30,31,31,30,31,30,31 };
		if (_month == 2 && ((_year % 4 == 0 && _year % 100 != 0) || _year % 400 == 0))
		{
			return 29;
		}
		return arr[month];
	}

1.2 Get the number of days per year

This operation is simpler than before. Just check whether it is a leap year. Without further explanation, just enter the code.

	int GetYearDay(int year)
	{
		if ((year % 4 == 0 && year % 100 != 0) || year % 400 == 0)
			return 366;
		return 365;
	}

1.3 Constructor, destructor and copy constructor

This aspect is very basic. I won’t go into details about the specific implementation process. If anyone doesn’t understand, you can read the blog written by the blogger before. C++ Beginner Classes and Objects (Part 2)-CSDN Blog C++ Elementary Classes and Objects (Part 2)-CSDN Blog

Note: The date constructed by the constructor may not be legal. You can use a judgment statement. If it is illegal, the program will be terminated.

Directly upload the code

	Date(int year=1,int month=1,int day=1)
	//构造函数
	{
		if (year < 1 || month>12 || day > GetMonthDay(year, month))
		{
			cout << "日期非法,创建失败" << endl;
			exit(-1);//退出程序
		}
		_year = year;
		_month = month;
		_day = day;
	}
	Date(const Date&d1)
	//拷贝构造函数
	{
		_year = d1._year;
		_month = d1._month;
		_day = d1._day;
	}
	~Date()
	//析构函数
	{
		_year = 0;
		_month = 0;
		_day = 0;
	}

2. Implementation of +,-,+=,-= for dates and days

2.1+= operator overloading

Goal: To achieve a date + number of days, the date is modified to the date of how many days have passed

Many people will write like this for the first time, and the compiler reports an error because there is an extra this pointer in the past, and the operation of the ternary operator has been reached. Therefore, we only need to remove Date d1, but because of the this pointer exists, we can still operate on this variable.

Before implementing this, we can write a GetMonthDay function to get the number of days in January. It is worth noting that February is 29th in leap years. Next, let’s talk about the implementation idea. Our return value is best set to Date& , because the target of the += operation still exists in main after leaving the function scope, so using the return value of the Date& type can improve the efficiency of the compiler.

You can first add day to the _day of the target, which can facilitate subsequent carry. For example, 2023-11-16 +100 is first processed into 2023-11-116, and then the carry is carried out through the specific carry data obtained by GetMonthDay. However, this can be achieved through a loop. When _day>GetMonthDay means a carry is required, so this can be used as a condition for whether the loop continues. Subsequent implementations only need to pay attention to some details, for example, when the month is 12 Enter 1 or something like that.

	int GetMonthDay(int year, int month)
	{
		int arr[13] = { 0,31,28,31,30,31,30,31,31,30,31,30,31 };
		//之所以是13是为了更加符合日期的返回,一月就返回数组下标为1的值
		if (month == 2 && ((year % 4 == 0 && year % 100 != 0) || year % 400 == 0))
		{
			return 29;
		}
		return arr[month];
	}
	Date& operator +=(int day)
	{
		_day += day;
		while (_day > GetMonthDay(_year,_month))
		{
			_day -= GetMonthDay(_year, _month);
			_month++;
			if (_month>12)
			{
				_month = 1;
				_year++;
			}
		}
		return *this;
	}

test: 

2.2+ operator implementation

After having the += operator, the + operator is easy to operate, just reuse it. 

The reason for using temporary variables is that the + operator does not change the value of the original variable. For example, a=b+100; b here will not be modified.

	Date operator+(int day)
	{
		Date tmp(*this);
		tmp += day;
		return tmp;
	}

2.3-Implementation of operator

 This implementation is similar to +=. You can first directly add -day to the object's _day, for example, 2023-11-17-100, then directly convert it to 2023-11-(-83), and then pass The carry of the month continuously increases _day until _day is greater than 0. The loop stops and returns the corresponding content.

Note: It is impossible for 0 to appear when the month and day number are --. You may have never heard of the expression 0 day or 0 month, so 0 should be taken into account in the conditional judgment statement.

Date& operator-=(int day)
	{
		_day -= day;
		while (_day <= 0)
		{
			_day += GetMonthDay(_year, _month);
			_month--;
			if (_month <= 0)
			{
				_month = 12;
				_year--;
			}
		}
		return *this;
	}

test:

2.4-Implementation of operators

Just reuse it as easily as the + operator

	Date operator-(int day)
	{
		Date tmp(*this);
		tmp -= day;
		return tmp;
	}

3. Implementation of ++, --

3.1 Implementation of pre-++ and post-++

It is very simple to do this step after having the += and + operators. The only thing you need to pay attention to is that the ++ operator with ++ after it needs an extra int in the parameter when overloading. Note: + in front +, use ++ first and then use it, use ++ after it first and then ++

For a more detailed explanation on this aspect, please see the article written by the blogger before. 

C++ Elementary Classes and Objects (Part 2)-CSDN Blog

	Date& operator++()
	//前置++,先++后使用
	{
		(*this) += 1;
		return *this;
	}
	Date operator++(int)
	//后置++,先使用后++
	{
		Date tmp(*this);
		*this += 1;
		return tmp;
	}

test:

3.2 Implementation of pre-- and post--

It is almost the same as the implementation of ++, so I won’t go into details again.

	Date& operator--()
	{
		(*this) -= 1;
		return *this;
	}
	Date operator--(int)
	{
		Date tmp(*this);
		*this -= 1;
		return tmp;
	}

test:

4. Preview of next issue

The content is a bit too much, so the blogger has split it into two articles, which will probably be posted tonight 

Guess you like

Origin blog.csdn.net/fq157856469/article/details/134449795