当前位置:网站首页>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
边栏推荐
- Certbot failed to update certificate solution
- 图应用详解
- 1291_ Add timestamp function in xshell log
- Global and Chinese markets for fire resistant conveyor belts 2022-2028: Research Report on technology, participants, trends, market size and share
- P2022 有趣的数(二分&数位dp)
- Query the number and size of records in each table in MySQL database
- Global and Chinese markets for endoscopic drying storage cabinets 2022-2028: Research Report on technology, participants, trends, market size and share
- Easyrecovery靠谱不收费的数据恢复电脑软件
- [FPGA tutorial case 11] design and implementation of divider based on vivado core
- Mixed development of QML and QWidget (preliminary exploration)
猜你喜欢
Interface idempotency
Stable Huawei micro certification, stable Huawei cloud database service practice
Comprehensive ability evaluation system
Yyds dry inventory automatic lighting system based on CC2530 (ZigBee)
Record an excel xxE vulnerability
Ipv4中的A 、B、C类网络及子网掩码
How to solve the problem of slow downloading from foreign NPM official servers—— Teach you two ways to switch to Taobao NPM image server
10 exemples les plus courants de gestion du trafic istio, que savez - vous?
Jd.com 2: how to prevent oversold in the deduction process of commodity inventory?
Solve the compilation problem of "c2001: line breaks in constants"
随机推荐
2327. 知道秘密的人数(递推)
Path of class file generated by idea compiling JSP page
MySql數據庫root賬戶無法遠程登陸解决辦法
Benefits of automated testing
2328. 网格图中递增路径的数目(记忆化搜索)
asp. Core is compatible with both JWT authentication and cookies authentication
Unity中几个重要类
Fedora/rehl installation semanage
The value of two date types is subtracted and converted to seconds
TCP/IP协议里面的网关地址和ip地址有什么区别?
Fundamentals of SQL database operation
Recommendation system (IX) PNN model (product based neural networks)
Introduction to hashtable
关于进程、线程、协程、同步、异步、阻塞、非阻塞、并发、并行、串行的理解
[face recognition series] | realize automatic makeup
P2648 make money
满足多元需求:捷码打造3大一站式开发套餐,助力高效开发
Basic use of MySQL (it is recommended to read and recite the content)
1291_ Add timestamp function in xshell log
Mlapi series - 04 - network variables and network serialization [network synchronization]