当前位置:网站首页>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 .
🧭 Navigate for you 🧭
🧨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
边栏推荐
- pd. to_ numeric
- P2648 make money
- [face recognition series] | realize automatic makeup
- User datagram protocol UDP
- 1291_ Add timestamp function in xshell log
- Mysql database storage engine
- C. The Third Problem(找规律)
- Stable Huawei micro certification, stable Huawei cloud database service practice
- Global and Chinese market of rubber wheel wedges 2022-2028: Research Report on technology, participants, trends, market size and share
- [FPGA tutorial case 11] design and implementation of divider based on vivado core
猜你喜欢

1291_ Add timestamp function in xshell log

Fundamentals of SQL database operation

How does computer nail adjust sound

MLAPI系列 - 04 - 网络变量和网络序列化【网络同步】

Lombok principle and the pit of ⽤ @data and @builder at the same time

Fedora/REHL 安装 semanage

View 工作流程

About some basic DP -- those things about coins (the basic introduction of DP)

Deep learning framework installation (tensorflow & pytorch & paddlepaddle)

Benefits of automated testing
随机推荐
Viewing and verifying backup sets using dmrman
BOM - location, history, pop-up box, timing
Guitar Pro 8.0最详细全面的更新内容及全部功能介绍
Jd.com 2: how to prevent oversold in the deduction process of commodity inventory?
Global and Chinese market of plasma separator 2022-2028: Research Report on technology, participants, trends, market size and share
MySql數據庫root賬戶無法遠程登陸解决辦法
Execution order of scripts bound to game objects
IDEA编译JSP页面生成的class文件路径
729. My schedule I (set or dynamic open point segment tree)
MySQL learning record 13 database connection pool, pooling technology, DBCP, c3p0
Yyds dry inventory automatic lighting system based on CC2530 (ZigBee)
Mlapi series - 04 - network variables and network serialization [network synchronization]
During pycharm debugging, the view is read only and pause the process to use the command line appear on the console input
VNCTF2022 WriteUp
tengine 内核参数
P2102 地砖铺设(dfs&贪心)
pd. to_ numeric
Leetcode32 longest valid bracket (dynamic programming difficult problem)
Overturn your cognition? The nature of get and post requests
10個 Istio 流量管理 最常用的例子,你知道幾個?