当前位置:网站首页>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
边栏推荐
- Jd.com 2: how to prevent oversold in the deduction process of commodity inventory?
- HotSpot VM
- R note prophet
- Web components series (VII) -- life cycle of custom components
- Yyds dry goods inventory web components series (VII) -- life cycle of custom components
- Lombok principle and the pit of ⽤ @data and @builder at the same time
- 食品行业仓储条码管理系统解决方案
- 牛顿插值法
- math_ Derivative function derivation of limit & differential & derivative & derivative / logarithmic function (derivative definition limit method) / derivative formula derivation of exponential functi
- Guitar Pro 8.0最详细全面的更新内容及全部功能介绍
猜你喜欢
CADD课程学习(7)-- 模拟靶点和小分子相互作用 (柔性对接 AutoDock)
JVM garbage collector concept
综合能力测评系统
Path of class file generated by idea compiling JSP page
Lombok原理和同时使⽤@Data和@Builder 的坑
Ipv4中的A 、B、C类网络及子网掩码
解决“C2001:常量中有换行符“编译问题
How does computer nail adjust sound
How to solve the problem of slow downloading from foreign NPM official servers—— Teach you two ways to switch to Taobao NPM image server
[adjustable delay network] development of FPGA based adjustable delay network system Verilog
随机推荐
SharedPreferences 源码分析
How to execute an SQL statement in MySQL
During pycharm debugging, the view is read only and pause the process to use the command line appear on the console input
2327. Number of people who know secrets (recursive)
DM8 archive log file manual switching
Implementation of knowledge consolidation source code 2: TCP server receives and processes half packets and sticky packets
729. 我的日程安排表 I(set or 动态开点线段树)
Mlapi series - 04 - network variables and network serialization [network synchronization]
npm命令--安装依赖包--用法/详解
How can programmers resist the "three poisons" of "greed, anger and ignorance"?
Solution to the problem that the root account of MySQL database cannot be logged in remotely
Class A, B, C networks and subnet masks in IPv4
Lora gateway Ethernet transmission
Python book learning notes - Chapter 09 section 01 create and use classes
C. The Third Problem(找规律)
10个 Istio 流量管理 最常用的例子,你知道几个?
2/13 qaq~~ greed + binary prefix sum + number theory (find the greatest common factor of multiple numbers)
Introduction to hashtable
Script lifecycle
Guitar Pro 8.0最详细全面的更新内容及全部功能介绍