当前位置:网站首页>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
边栏推荐
- Practical development of member management applet 06 introduction to life cycle function and user-defined method
- What is the difference between gateway address and IP address in tcp/ip protocol?
- Stable Huawei micro certification, stable Huawei cloud database service practice
- DM8 archive log file manual switching
- Ipv4中的A 、B、C类网络及子网掩码
- Viewing and verifying backup sets using dmrman
- 10个 Istio 流量管理 最常用的例子,你知道几个?
- Stable Huawei micro certification, stable Huawei cloud database service practice
- 2/13 review Backpack + monotonic queue variant
- Yyds dry goods inventory web components series (VII) -- life cycle of custom components
猜你喜欢
User datagram protocol UDP
图应用详解
[adjustable delay network] development of FPGA based adjustable delay network system Verilog
R note prophet
VNCTF2022 WriteUp
10个 Istio 流量管理 最常用的例子,你知道几个?
Comprehensive ability evaluation system
Yyds dry inventory automatic lighting system based on CC2530 (ZigBee)
Lombok principle and the pit of ⽤ @data and @builder at the same time
CertBot 更新证书失败解决
随机推荐
About some basic DP -- those things about coins (the basic introduction of DP)
绑定在游戏对象上的脚本的执行顺序
Certbot failed to update certificate solution
math_ Derivative function derivation of limit & differential & derivative & derivative / logarithmic function (derivative definition limit method) / derivative formula derivation of exponential functi
【HBZ分享】云数据库如何定位慢查询
P2022 interesting numbers (binary & digit DP)
Unity中几个重要类
Path of class file generated by idea compiling JSP page
记一次excel XXE漏洞
Lombok principle and the pit of ⽤ @data and @builder at the same time
关于进程、线程、协程、同步、异步、阻塞、非阻塞、并发、并行、串行的理解
Global and Chinese markets for patent hole oval devices 2022-2028: Research Report on technology, participants, trends, market size and share
Understanding of processes, threads, coroutines, synchronization, asynchrony, blocking, non blocking, concurrency, parallelism, and serialization
10個 Istio 流量管理 最常用的例子,你知道幾個?
How does technology have the ability to solve problems perfectly
Basic use of MySQL (it is recommended to read and recite the content)
1291_Xshell日志中增加时间戳的功能
DM8 backup set deletion
Query the number and size of records in each table in MySQL database
Codeforces Round #770 (Div. 2) B. Fortune Telling