当前位置:网站首页>Prince language on insect date class
Prince language on insect date class
2022-06-23 04:22:00 【Hua Weiyun】
Date class
We should have the habit of writing code or projects == Write a little, make up a little ==, Form good habits , It is absolutely forbidden to edit after writing , Then he made a lot of mistakes and didn't want to see them , We first compile a minimum project or system framework to add other functions
Date.h
Date.hSome file headers , Statement or something
#pragma once#include <iostream>using std::cout;using std::cin;using std::endl;class Date{public: Date(int year = 0, int month = 1, int day = 1); void Print(); // Like deconstruction , Copy structure , Assignment overloading can be done without writing , Because the default generation is enough , // image Stack You need to write these three // Date plus Minus days Date operator+(const int& day); Date operator-(const int& day);private: int _year; int _month; int _day;};
Date.cpp
Date.cppThis is the function to write the date class
#include "Date.h"Date::Date(int year, int month, int day){ _year = year; _month = month; _day = day;}void Date::Print(){ cout << _year << " year " << _month << " month " << _day << " Japan " << endl;}Date Date::operator+(const int& day){ return *this;}Date Date::operator-(const int& day){ return *this;}
test.cpp
test.cpp
#include "Date.h"int main(){ Date d(2022,1,1); d.Print(); return 0;}
Test situation
Function add
When we can get out of the basic beggar version, we still have to go on the next road , We should consider whether the data is reasonable
== So we need to check the validity of the date ==
== The above does exclude percent 99 It's filtered out , But leap years 2 Month is a special consideration, but there is no code implementation ==
== Can we optimize , For example, if I input a wrong date and you die directly, is it a bit overbearing , You should throw an exception , Or an interval or something ==
inline int GetMonthDay(int year, int month){ // The array stores the number of days of each month in a normal year The corresponding subscript is month The element inside is heaven static int dayArray[13] = { 0,31,28,31,30,31,30,31,31,30,31,30,31 }; // Days of the month int day = dayArray[month]; // Leap year is 4 Once a year, once a century, or once every 400 years if (month == 2 && (year % 4 == 0 && year % 100 != 0) || year % 400 == 0) { // Leap year's 2 Month is 29 God day = 29; } return day;}Date::Date(int year, int month, int day){ // Check the validity of the date if (year > 0 && month > 0 && month <13 && day > 0 && day <= GetMonthDay(year,month)) { _year = year; _month = month; _day = day; } else { cout << endl; cout << " Illegal date " << endl; cout << year << " year " << month << " month " << day << " Japan " << endl; }}void Date::Print(){ cout << _year << " year " << _month << " month " << _day << " Japan " << endl;}
The date after the date plus the number of days
Date& Date::operator+=(const int& day){ // We don't care , Let's add the sky directly _day += day; // Then judge whether it is legal while (_day > GetMonthDay(_year,_month)) { // First, subtract the number of days in the current month _day -= GetMonthDay(_year, _month); // Next month ++ _month++; // If the month has passed, it will be a year ++ if (_month > 12) { _year++; _month = 1; } } return *this;}
Date plus days
== It's just adding and not assigning , You have to distinguish yourself from the one above , So I need an intermediate temporary variable ( We call this temporary object ) To store the calculated data ==
// Date plus days Don't give it back Date Date::operator+(const int& day){ // First create a temporary object Copy the previous copy to him first Date ret(*this); // Reuse += ret += day; return ret;}
date ++,++ date
== Both date plus and date plus are ++, however operator How to distinguish between operator overloads ,==
In front of ++
== In front of ++ Returns the value after the operation ==
//++d Date before ++ Converted to d.operator++(&d)Date& Date::operator++(){ // Returns the value after the operation *this += 1; return *this;}
After ++
== After ++ Returns the value before the operation ==
//d++ Post date ++ Converted to d.operator++(&d,0)// there int It's just space , You don't need to give arguments , Play the role of function overloading Date& Date::operator++(int){ // After ++ Returns the value before the operation // So you need to save a temporary object first Date tmp(*this); *this += 1; return tmp;}
The date minus the number of days
// The date minus the number of days is assigned back at the same time Date& Date::operator-=(const int& day){ // Say nothing more , Subtract the days first _day -= day; // If it's not legal, wait until it's legal while (_day <= 0) { // First reduce the number of months _month--; // First judge whether the month is zero , If yes, it will be years of operation if (_month <= 0) { _year--; _month = 12; } // Then ask him to add the correct number of days in the month _day += GetMonthDay(_year,_month); } return *this;}
Date minus days
== Just reuse the above function ==
// Date minus days Don't give it back Date Date::operator-(const int& day){ Date ret(*this); //-= Reuse of ret -= day; return ret;}
date –,-- date
== Both date subtraction and subtraction date are –, however operator How to distinguish between operator overloads ,==
In front of –
== In front of – Returns the value after the operation ==
// Pre subtraction Date& Date::operator--(){ *this -= 1; return *this;}
After –
== After – Returns the value before the operation ==
// Post subtraction Date& Date::operator--(int){ Date tmp(*this); *this -= 1; return tmp;}
边栏推荐
- Prince language under insect date category
- 虫子 STM32 高级定时器 (哈哈我说实话硬件定时器不能体现实力,实际上想把内核定时器发上来的,一想算了,慢慢来吧)
- 在 KubeSphere 上部署 Apache Pulsar
- JD cloud distributed database stardb won the "stability practice pioneer" of China Academy of information technology
- 1-1 introduction to VMWare
- Half search method
- Avltree - arbre de recherche binaire équilibré
- [tcapulusdb knowledge base] [list table] sample code for inserting data into the specified position in the list
- Differences between MyISAM and InnoDB of MySQL storage engine
- 最新编程语言排行榜
猜你喜欢

【二叉树进阶】AVLTree - 平衡二叉搜索树

1-1 introduction to VMWare

photoshop PS 查看像素坐标、像素颜色、像素HSB颜色

Twitter与Shopify合作 将商家产品引入Twitter购物当中

SVG+JS智能家居监控网格布局

基于FPGA的VGA协议实现

在word里,如何让页码从指定页开始编号

高效的远程办公经验 | 社区征文

京东云分布式数据库StarDB荣获中国信通院 “稳定性实践先锋”

JD cloud distributed database stardb won the "stability practice pioneer" of China Academy of information technology
随机推荐
关于sql语句的问题
linux下的开源数据库是什么
在线文本过滤小于指定长度工具
[machine learning] wuenda's machine learning assignment ex2 logistic regression matlab implementation
D overloading nested functions
【深度学习】深度学习推理框架 TensorRT MNN OpenVINO ONNXRuntime
Common events for elements
Navar's Treasure Book: the principle of getting rich without luck
城链科技董事长肖金伟:践行数据经济系国家战略,引领数字时代新消费发展!
华为联机对战服务玩家快速匹配后,不同玩家收到的同一房间内玩家列表不同
PTA:7-69 数据的间距问题
[OWT] OWT client native P2P E2E test vs2017 construction 4: Construction and link of third-party databases p2pmfc exe
[tcapulusdb knowledge base] [list table] example code of batch deleting data at specified location in the list
[OWT] OWT client native P2P E2E test vs2017 build 3: no test unit comparison, manually generate vs projects
Avltree - arbre de recherche binaire équilibré
bubble sort
深度学习 简介
Talk about memory model and memory order
[tcapulusdb knowledge base] [list table] delete all data sample codes in the list
What is the difference between redistemplate and CacheManager operation redis


















