当前位置:网站首页>After learning classes and objects, I wrote a date class

After learning classes and objects, I wrote a date class

2022-07-06 04:19:00 You are handsome, you say it first

●🧑 Personal home page : You're handsome. You say it first .
● Welcome to like, pay attention to collection
● Both chose the distance , We will only struggle ahead .
●🤟 You are welcome to write to me if you have any questions !
●🧐 Copyright : This paper is written by [ You're handsome. You say it first .] original ,CSDN First episode , Infringement must be investigated .

🧨1. Design thinking

The date class we designed this time is about to realize Judging the size of the date Addition and subtraction of date Input and output of date The date corresponds to the day of the week . This will put us in front Classes and objects Apply the knowledge points learned in , Let's have a deeper understanding of classes and objects .

2.Date Class framework

Date.h

#pragma once
#include<iostream>
using namespace std;
class Date
{
    
	//  Friend function 
	friend ostream& operator<<(ostream& out, const Date& d);
	friend istream& operator>>(istream& in, Date& d);
public:
	Date(int year = 1, int month = 1, int day = 1);
	void Print() const;
	int GetMonthDay(int year, int month) 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;
	bool operator==(const Date& d)const;
	bool operator!=(const Date& d)const;
	Date& operator+=(int day);
	Date operator+(int day)const;
	Date& operator-=(int day);
	Date operator-(int day)const;
	Date& operator++();
	Date operator++(int);
	Date& operator--();
	Date operator--(int);
	int operator-(const Date& d) const;
	void PrintWeekDay() const;
private:
	int _year;
	int _month;
	int _day;
};

3. Class member function implementation

These implementations are placed in Date.cpp In the document
The first interface to be implemented is Constructors

void Date::Print() const
{
    
	cout << _year << "-" << _month << "-" << _day << endl;
}
Date::Date(int year, int month, int day)
{
    
	_year = year;
	_month = month;
	_day = day;

	if (!(_year >= 0
		&& (month > 0 && month < 13)
		&& (day > 0 && day <= GetMonthDay(year, month))))
	{
    
		cout << " Illegal date ->";
		Print();
	}
}

Next we're going to achieve Date The core interface in the class :GetMonthDay, This interface when you enter year and month when , It will return the corresponding number of days , Here is the problem of weekdays and leap years .C At the language stage, we learned to judge leap years , There is a pithy formula : A leap in four years , A hundred years without Leap , Four hundred years later , To implement it in code is
year % 4 == 0 && year % 100 != 0 || year % 400 == 0

int Date::GetMonthDay(int year, int month) const
{
    
	static int monthDayArray[13] = {
     0, 31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31 };
	int day = monthDayArray[month];
	if (month == 2 && ((year % 4 == 0 && year % 100 != 0) || (year % 400 == 0)))
	{
    
		day += 1;
	}

	return day;
}

Complete the implementation of this interface , The next interface is easy .
heavy load > Operator

bool Date::operator>(const Date& d)const
{
    
	if (_year > d._year)
	{
    
		return true;
	}
	else if (_year == d._year && _month > d._month)
	{
    
		return true;
	}
	else if (_year == d._year && _month == d._month && _day > d._day)
	{
    
		return true;
	}
	else
	{
    
		return false;
	}
}

heavy load == Operator

bool Date::operator==(const Date& d)const
{
    
	return _year == d._year
		&& _month == d._month
		&& _day == d._day;
}

🥎 heavy load < Operator

bool Date::operator<(const Date& d)  const
{
    
	return !(*this >= d);
}

heavy load >= Operator

bool Date::operator>=(const Date& d)  const
{
    
	return *this > d || *this == d;
}

heavy load <= Operator

bool Date::operator<=(const Date& d)  const
{
    
	return  !(*this > d);
}

heavy load != Operator

bool Date::operator!=(const Date& d)  const
{
    
	return  !(*this == d);
}

🤿 heavy load += Operator

Date& Date::operator+=(int day)
{
    
	if (day < 0)
	{
    
		return *this -= -day;
	}

	_day += day;
	while (_day > GetMonthDay(_year, _month))
	{
    
		_day -= GetMonthDay(_year, _month);
		++_month;

		if (_month == 13)
		{
    
			_month = 1;
			_year++;
		}
	}

	return *this;
}

heavy load + Operator

Date Date::operator+(int day)  const
{
    
	Date ret(*this);
	//ret.operator+=(day);
	ret += day;

	return ret;
}

🥌 heavy load -= Operator

Date& Date::operator-=(int day)
{
    
	if (day < 0)
	{
    
		return *this += -day;
	}

	_day -= day;
	while (_day <= 0)
	{
    
		--_month;
		if (_month == 0)
		{
    
			--_year;
			_month = 12;
		}

		_day += GetMonthDay(_year, _month);
	}

	return *this;
}

heavy load - Operator

// Calculate the date after subtracting a few days 
Date Date::operator-(int day)  const
{
    
	Date ret(*this);
	ret -= day;

	return ret;
}
// Calculate the date difference 
int Date::operator-(const Date& d)  const
{
    
	Date max = *this;
	Date min = d;
	int flag = 1;

	if (*this < d)
	{
    
		max = d;
		min = *this;
		flag = -1;
	}

	int count = 0;
	while (min != max)
	{
    
		++min;
		++count;
	}

	return count * flag;
}

heavy load In front of ++ Operator

Date& Date::operator++()
{
    
	*this += 1;

	return *this;
}

heavy load After ++ Operator

Date Date::operator++(int)
{
    
	Date ret(*this);
	*this += 1;

	return ret;
}

heavy load In front of -- Operator

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

🪀 heavy load After -- Operator

Date Date::operator--(int)
{
    
	Date ret(*this);
	*this -= 1;
	return ret;
}

Output day of the week

void Date::PrintWeekDay() const
{
    
	const char* arr[] = {
     " Monday ", " Tuesday ", " Wednesday ", " Thursday ", " Friday ", " Saturday ", " Sunday " };
	//Date start(1900, 1, 1);
	//int count = *this - start;
	int count = *this - Date(1900, 1, 1);

	cout << arr[count % 7] << endl;
}

🪅 heavy load << Operator

ostream& operator<<(ostream& out, const Date& d)
{
    
	out << d._year << "/" << d._month << "/" << d._day << endl;
	return out;
}

heavy load >> Operator

istream& operator>>(istream& in,Date& d)
{
    
	in >> d._year >> d._month >> d._day;
	return in;
}

Date.h and Date.cpp After the content in the document is realized , The whole date class is finished . Then we can Test.cpp The file calls and runs . I think it's too simple , It can also be in Test.cpp Write a menu in .

Those who like this article can give One key, three links Like, focus on collection

原网站

版权声明
本文为[You are handsome, you say it first]所创,转载请带上原文链接,感谢
https://yzsam.com/2022/02/202202132232168648.html