C ++ programming entry - Date Class operator overloading

Title: Custom Date class Date, including year, month, day three member variables, in which an overloaded operator "+", "-", "+", "-." (Note: You need to consider a different number of days per month and leap year issue).

Entitled "Object-Oriented Programming" (C ++ language) - Lei Cheng Li Aihua after-school textbook chapter VII of title


Well, we have to analyze the topic of the class declaration functional orientation, is a heavy-duty four operators, where we follow the characteristics of the C ++ package, using heavy-duty as a member function approach.

Shaped like:

T operator @ (parameter list) {

// function body

}

Wherein T is a return type, class type typically, operator keywords, we want @ is overloaded operator, depending on the parameter table to see our case, binocular and monocular points.

Combined with our look at the specific topics to achieve what functions:

"+": After a given date, call the "+" operator overloaded function, then the date is added to the parameters given day, for example:

Date operator+(int days);//实现将日期加到days天之后

Similarly, the "-" before the date of return to form was the number of trees, "++" for the date plus one day, "-" for the date minus one day, we look at their statement

Date operator++(int);   //单目,日期加一
Date operator--(int);   //单目,日期减一
Date operator+(int days);   //双目,日期加days
Date operator-(int days);   //双目,日期减days

It appears to be very simple, but we have to consider the issue after a number of days in the realization of addition and subtraction, such as the number of days not months of January and February, as the number of days in January added 31 before returning 1, months increased to 2,2 month time will have to consider whether a leap year, and then decide the number of days to 28 or 29 return next month 1, where we define a function member function to determine a leap year:

bool IsLeapYear(int year){        //判断是否为闰年
		if (year % 4 == 0 && year % 100 != 0 || year % 400 == 0)
		{
			return true;
		}
		return false;
	}

So we have the specific number of days into the cycle operator overloading which function to achieve circulating we look at the full class declaration:

class Date{
private:
	int month, day, year;
public:
	Date(int y=2019, int m=11, int d=2):year(y),month(m),day(d);
	Date(const Date &d);
	~Date();

	Date operator++(int);      //日期加1,重载后置++
	Date operator--(int);     //日期减1,重载后置--
	Date operator+(int days);    //days天之后的日期
	Date operator-(int days);    //days天之前的日期
	bool IsLeapYear(int year);        //判断是否为闰年
	void ShowDate();         //显示日期,格式如2000-10-01

First define operator ++, we only need to add a few cycles, the month can be handled separately, divided into three categories, namely 1, 3, 5, 7, 8, 10, 31 days of December, the number of days that is greater than 31 returns 1 months plus 1, and wherein the bit special December, if the number of days is greater than 31 December, 1 is added to the year, month and number of days return 1; second category is 4,6,9, November, greater than 30 days, i.e. February 1 can be added, the number of days to return 1; last February, you need to determine whether a leap year, so, 29 days return cycle, no, 28 days month cycle.

Date Date::operator++(int){      //日期加1,重载后置++
			switch (month)
			{
			case 1:
			case 3:
			case 5:
			case 7:
			case 8:
			case 10:
				if (day<31)   {day += 1;}
				else { day = 1; month += 1; }
				break;
			case 12:
				if (day < 31) {
					day += 1;
				}
				else {
					day = 1;
					month = 1;
					year += 1;
				}
				break;
			case 4:
			case 6:
			case 9:
			case 11:
				if (day < 30)  {day += 1;}
				else {
					day = 1;
					month += 1;
				}
				break;
			case 2:
				if (IsLeapYear(year)){
					if (day < 29) day += 1;
					else {
						day = 1; month += 1;
					}
				}
				else if (day<28){ day += 1; }
				else
				{
					day = 1; month += 1;
				}
				break;
			}
			return *this;
			
		}
	Date Date::operator--(int){
		switch (month)
		{
		case 1:
			if (day == 1) { year -= 1; month = 12; day = 31; }
			else day -= 1;
			break;
		case 3:
			if (day != 1) day -= 1;
			else {
				if (IsLeapYear(year)) { day = 29; month = 2; }
				else { day = 28; month = 2; }
			}
		case 2:
		case 4:
		case 6:
		case 8:
		case 9:
		case 11:
			if (day != 1) day -= 1;
			else { day = 31; month -= 1;}
			break;
		case 5:
		case 7:
		case 10:
		case 12:
			if (day != 1) day -= 1;
			else { day = 30; month -= 1; }
			break;
		}
		return *this;
	}

After these two definitions, the other is simple, we look at "+" and "-." The way to achieve along with the other functions:

Date Date::operator+(int days)    //days天之后的日期
	{
		Date temp(*this);
		for (int i = 0; i < days; i++)
		{
			temp++;
		}
		return temp;
	}
	Date Date::operator-(int days)    //days天之前的日期
	{
		Date temp(*this);
		for (int i = 0; i < days; i++)
		{
			temp--;
		}
		return temp;
	}
	bool Date::IsLeapYear(int year){        //判断是否为闰年
		if (year % 4 == 0 && year % 100 != 0 || year % 400 == 0)
		{
			return true;
		}
		return false;
	}
	void Date::ShowDate(){          //显示日期,格式如2000-10-01
		cout << year << "-" << month << "-" << day << endl;
	}

As described in this chapter show achieve operator overloading and Date class, it did not involve other complex knowledge, so we went to the main test function which is given directly to functional testing of this class:

int main(){
	Date d1, d2(1999, 5, 25), d3;
	cout << "d1日期:";
	d1.ShowDate();
	cout << "d2日期:";
	d2.ShowDate();

	d3=d1++;
	cout << "d1日期++:";
	d3.ShowDate();
	d3 = d2--;
	cout << "d2日期--:";
	d3.ShowDate();

	d3 = d1 + 20;
	cout << "d1日期+20天:";
	d3.ShowDate();
	d3 = d2 - 20;
	cout << "d2日期-20天:";
	d3.ShowDate();
	return 0;
}

The following procedures will be integrated, because the program is short, regardless of the file on the program, given directly .cpp file to run the code:

//created by kong at 2019-11-02
#include<iostream>
using namespace std;

class Date{
private:
	int month, day, year;
public:
	Date(int y=2019, int m=11, int d=2):year(y),month(m),day(d){ }
	Date(const Date &d)    
		{year = d.year; month = d.month; day = d.day;}
	~Date(){}

	Date operator++(int){      //日期加1,重载后置++
			switch (month)
			{
			case 1:
			case 3:
			case 5:
			case 7:
			case 8:
			case 10:
				if (day<31)   {day += 1;}
				else { day = 1; month += 1; }
				break;
			case 12:
				if (day < 31) {
					day += 1;
				}
				else {
					day = 1;
					month = 1;
					year += 1;
				}
				break;
			case 4:
			case 6:
			case 9:
			case 11:
				if (day < 30)  {day += 1;}
				else {
					day = 1;
					month += 1;
				}
				break;
			case 2:
				if (IsLeapYear(year)){
					if (day < 29) day += 1;
					else {
						day = 1; month += 1;
					}
				}
				else if (day<28){ day += 1; }
				else
				{
					day = 1; month += 1;
				}
				break;
			}
			return *this;
			
		}
	Date operator--(int){
		switch (month)
		{
		case 1:
			if (day == 1) { year -= 1; month = 12; day = 31; }
			else day -= 1;
			break;
		case 3:
			if (day != 1) day -= 1;
			else {
				if (IsLeapYear(year)) { day = 29; month = 2; }
				else { day = 28; month = 2; }
			}
		case 2:
		case 4:
		case 6:
		case 8:
		case 9:
		case 11:
			if (day != 1) day -= 1;
			else { day = 31; month -= 1;}
			break;
		case 5:
		case 7:
		case 10:
		case 12:
			if (day != 1) day -= 1;
			else { day = 30; month -= 1; }
			break;
		}
		return *this;
	}

	Date operator+(int days)    //days天之后的日期
	{
		Date temp(*this);
		for (int i = 0; i < days; i++)
		{
			temp++;
		}
		return temp;
	}
	Date operator-(int days)    //days天之前的日期
	{
		Date temp(*this);
		for (int i = 0; i < days; i++)
		{
			temp--;
		}
		return temp;
	}
	bool IsLeapYear(int year){        //判断是否为闰年
		if (year % 4 == 0 && year % 100 != 0 || year % 400 == 0)
		{
			return true;
		}
		return false;
	}
	void ShowDate(){          //显示日期,格式如2000-10-01
		cout << year << "-" << month << "-" << day << endl;
	}
};

int main(){
	Date d1, d2(1999, 5, 25), d3;
	cout << "d1日期:";
	d1.ShowDate();
	cout << "d2日期:";
	d2.ShowDate();

	d3=d1++;
	cout << "d1日期++:";
	d3.ShowDate();
	d3 = d2--;
	cout << "d2日期--:";
	d3.ShowDate();

	d3 = d1 + 20;
	cout << "d1日期+20天:";
	d3.ShowDate();
	d3 = d2 - 20;
	cout << "d2日期-20天:";
	d3.ShowDate();
	return 0;
}

operation result:

Published 51 original articles · won praise 5 · Views 2038

Guess you like

Origin blog.csdn.net/MARS_098/article/details/102900635