Date类实现(c++)

2024-06-22 03:04
文章标签 date c++ 实现

本文主要是介绍Date类实现(c++),希望对大家解决编程问题提供一定的参考价值,需要的开发者们随着小编来一起学习吧!

文章目录

  • 前言
  • 总体框架
  • Date(int year=1, int month=1, int day=1);
  • ~Date();
  • Date(const Date& d);
  • Date& operator=(const Date& d);
  • int getMonthDay(int year, int month);
  • void Print()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);
  • Date operator-(int day);
  • Date& operator-=(int day);
  • int operator-(const Date&d);
  • Date& operator++();
  • Date operator++(int);
  • Date& operator--();
  • Date operator--(int);
  • 总结


前言

我们本篇内容将会对Date类进行模拟实现

总体框架

class Date
{
public:
private:int _year=1;int _month=1;int _day=1;
};

Date(int year=1, int month=1, int day=1);

拷贝构造,判断传值是否合法

Date::Date(int year, int month, int day)
{if (year >= 1 && month >= 1 && month <= 12 && day >= 1 && day <= getMonthDay(year, month)){_year = year;_month = month;_day = day;}else{assert(false);}
}

~Date();

析构函数,这里不涉及资源,我们可以不写

Date::~Date()
{}

Date(const Date& d);

拷贝构造,浅拷贝

Date::Date(const Date& d)
{_year = d._year;_month = d._month;_day = d._day;
}

Date& operator=(const Date& d);

赋值

Date& Date::operator=(const Date& d)
{if (this != &d){_year = d._year;_month = d._month;_day = d._day;}return *this;
}

int getMonthDay(int year, int month);

获取每月的天数,方便计算。

int Date::getMonthDay(int year, int month)
{int arr[13] = { 0,31,28,31,30,31,30,31,31,30 ,31,30,31 };//闰年:4年一闰年且百年不润年   400年一闰年if (month==2&&((year % 4 == 0 && year % 100 != 0) || (year % 400 == 0))){return 29;}return arr[month];
}

void Print()const;

进行简单打印,

void Date::Print()const
{cout << _year << " " << _month << " " << _day << endl;
}

bool operator>(const Date& d)const;


bool  Date::operator>(const Date& d)const
{if (_year > d._year){return true;}if (_year == d._year && _month > d._month){return true;}if (_year == d._year && _month > d._month && _day > d._day){return true;}return false;
}

bool operator==(const Date& d)const;

bool  Date::operator==(const Date& d)const
{return _year == d._year && _month == d._month && _day == d._day;
}

bool operator<(const Date& d)const;

后面几个接口都是根据前面两个接口进行复用

bool  Date::operator<(const Date& d)const
{return !(*this > d) || (*this == d);
}

bool operator!=(const Date& d)const;

bool  Date::operator!=(const Date& d)const
{return !(*this == d);
}

bool operator>=(const Date& d)const;

bool  Date::operator>=(const Date& d)const
{return (*this > d) || (*this == d);
}

bool operator<=(const Date& d)const;

bool  Date::operator<=(const Date& d)const
{return !(*this > d);
}

Date operator+(int day);

Date Date::operator+(int day)
{Date tmp(*this);tmp += day;return tmp;
}

Date& operator+=(int day);

我们这里选用+=复用+,而不是用+复用+=。
可以减少拷贝,提高效率。

Date& Date::operator+=(int day)
{_day += day;while (_day > getMonthDay(_year, _month)){_day -= getMonthDay(_year, _month);_month++;if (_month == 13){_year++;_month = 1;}}return *this;
}

Date operator-(int day);

Date Date::operator-(int day)
{Date tmp(*this);tmp -= day;return tmp;
}

Date& operator-=(int day);

Date& Date::operator-=(int day)
{_day -= day;while (_day <= 0){//+上个月的_month--;if (_month == 0){_year--;_month = 12;}_day += getMonthDay(_year, _month);}return *this;
}

int operator-(const Date&d);

两个日期相减,我们直接进行减是不容易操作的。

int Date::operator-(const Date& d)
{int flag = 1;Date min = *this;Date max = d;if (*this > d){min = d;max = *this;flag = -1;}int n = 0;while (min != max){if (n == 19){int i = 0;}++n;++min;}return n*flag;
}

Date& operator++();

Date& Date::operator++()
{*this +=  1;return *this;
}

Date operator++(int);

Date Date::operator++(int)
{Date tmp(*this);*this = *this + 1;return tmp;
}

Date& operator–();

Date& Date::operator--()
{*this = *this -1;return *this;
}

Date operator–(int);

Date Date::operator--(int)
{Date tmp(*this);*this = *this - 1;return tmp;
}

整体代码

#include <iostream>
using namespace std;
#include <assert.h>
class Date
{
public:Date(int year=1, int month=1, int day=1);~Date();Date(const Date& d);Date& operator=(const Date& d);int getMonthDay(int year, int month);void Print()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);Date operator-(int day);Date& operator-=(int day);int operator-(const Date&d);Date& operator++();Date operator++(int);Date& operator--();Date operator--(int);private:int _year=1;int _month=1;int _day=1;
};
#include "Date.h"int Date::getMonthDay(int year, int month)
{int arr[13] = { 0,31,28,31,30,31,30,31,31,30 ,31,30,31 };//闰年:4年一闰年且百年不润年   400年一闰年if (month==2&&((year % 4 == 0 && year % 100 != 0) || (year % 400 == 0))){return 29;}return arr[month];
}void Date::Print()const
{cout << _year << " " << _month << " " << _day << endl;
}Date::Date(int year, int month, int day)
{if (year >= 1 && month >= 1 && month <= 12 && day >= 1 && day <= getMonthDay(year, month)){_year = year;_month = month;_day = day;}else{assert(false);}
}
Date::~Date()
{
}Date::Date(const Date& d)
{_year = d._year;_month = d._month;_day = d._day;
}Date& Date::operator=(const Date& d)
{if (this != &d){_year = d._year;_month = d._month;_day = d._day;}return *this;
}bool  Date::operator>(const Date& d)const
{if (_year > d._year){return true;}if (_year == d._year && _month > d._month){return true;}if (_year == d._year && _month > d._month && _day > d._day){return true;}return false;
}
bool  Date::operator==(const Date& d)const
{return _year == d._year && _month == d._month && _day == d._day;
}
bool  Date::operator<(const Date& d)const
{return !(*this > d) || (*this == d);
}
bool  Date::operator>=(const Date& d)const
{return (*this > d) || (*this == d);
}
bool  Date::operator!=(const Date& d)const
{return !(*this == d);
}
bool  Date::operator<=(const Date& d)const
{return !(*this > d);
}//d1+100
Date Date::operator+(int day)
{Date tmp(*this);tmp += day;return tmp;
}
Date& Date::operator+=(int day)
{_day += day;while (_day > getMonthDay(_year, _month)){_day -= getMonthDay(_year, _month);_month++;if (_month == 13){_year++;_month = 1;}}return *this;
}
Date Date::operator-(int day)
{Date tmp(*this);tmp -= day;return tmp;
}
Date& Date::operator-=(int day)
{_day -= day;while (_day <= 0){//+上个月的_month--;if (_month == 0){_year--;_month = 12;}_day += getMonthDay(_year, _month);}return *this;
}Date& Date::operator++()
{*this +=  1;return *this;
}
Date Date::operator++(int)
{Date tmp(*this);*this = *this + 1;return tmp;
}
Date& Date::operator--()
{*this = *this -1;return *this;
}
Date Date::operator--(int)
{Date tmp(*this);*this = *this - 1;return tmp;
}int Date::operator-(const Date& d)
{int flag = 1;Date min = *this;Date max = d;if (*this > d){min = d;max = *this;flag = -1;}int n = 0;while (min != max){if (n == 19){int i = 0;}++n;++min;}return n*flag;
}

总结

以上就是今天要讲的内容。希望对大家的学习有所帮助,仅供参考 如有错误请大佬指点我会尽快去改正 欢迎大家来评论~~ 😘 😘 😘

这篇关于Date类实现(c++)的文章就介绍到这儿,希望我们推荐的文章对编程师们有所帮助!



http://www.chinasem.cn/article/1083109

相关文章

关于C++中的虚拟继承的一些总结(虚拟继承,覆盖,派生,隐藏)

1.为什么要引入虚拟继承 虚拟继承是多重继承中特有的概念。虚拟基类是为解决多重继承而出现的。如:类D继承自类B1、B2,而类B1、B2都继承自类A,因此在类D中两次出现类A中的变量和函数。为了节省内存空间,可以将B1、B2对A的继承定义为虚拟继承,而A就成了虚拟基类。实现的代码如下: class A class B1:public virtual A; class B2:pu

C++对象布局及多态实现探索之内存布局(整理的很多链接)

本文通过观察对象的内存布局,跟踪函数调用的汇编代码。分析了C++对象内存的布局情况,虚函数的执行方式,以及虚继承,等等 文章链接:http://dev.yesky.com/254/2191254.shtml      论C/C++函数间动态内存的传递 (2005-07-30)   当你涉及到C/C++的核心编程的时候,你会无止境地与内存管理打交道。 文章链接:http://dev.yesky

C++的模板(八):子系统

平常所见的大部分模板代码,模板所传的参数类型,到了模板里面,或实例化为对象,或嵌入模板内部结构中,或在模板内又派生了子类。不管怎样,最终他们在模板内,直接或间接,都实例化成对象了。 但这不是唯一的用法。试想一下。如果在模板内限制调用参数类型的构造函数会发生什么?参数类的对象在模板内无法构造。他们只能从模板的成员函数传入。模板不保存这些对象或者只保存他们的指针。因为构造函数被分离,这些指针在模板外

C++工程编译链接错误汇总VisualStudio

目录 一些小的知识点 make工具 可以使用windows下的事件查看器崩溃的地方 dumpbin工具查看dll是32位还是64位的 _MSC_VER .cc 和.cpp 【VC++目录中的包含目录】 vs 【C/C++常规中的附加包含目录】——头文件所在目录如何怎么添加,添加了以后搜索头文件就会到这些个路径下搜索了 include<> 和 include"" WinMain 和

C/C++的编译和链接过程

目录 从源文件生成可执行文件(书中第2章) 1.Preprocessing预处理——预处理器cpp 2.Compilation编译——编译器cll ps:vs中优化选项设置 3.Assembly汇编——汇编器as ps:vs中汇编输出文件设置 4.Linking链接——链接器ld 符号 模块,库 链接过程——链接器 链接过程 1.简单链接的例子 2.链接过程 3.地址和

C++必修:模版的入门到实践

✨✨ 欢迎大家来到贝蒂大讲堂✨✨ 🎈🎈养成好习惯,先赞后看哦~🎈🎈 所属专栏:C++学习 贝蒂的主页:Betty’s blog 1. 泛型编程 首先让我们来思考一个问题,如何实现一个交换函数? void swap(int& x, int& y){int tmp = x;x = y;y = tmp;} 相信大家很快就能写出上面这段代码,但是如果要求这个交换函数支持字符型

通过SSH隧道实现通过远程服务器上外网

搭建隧道 autossh -M 0 -f -D 1080 -C -N user1@remotehost##验证隧道是否生效,查看1080端口是否启动netstat -tuln | grep 1080## 测试ssh 隧道是否生效curl -x socks5h://127.0.0.1:1080 -I http://www.github.com 将autossh 设置为服务,隧道开机启动

时序预测 | MATLAB实现LSTM时间序列未来多步预测-递归预测

时序预测 | MATLAB实现LSTM时间序列未来多步预测-递归预测 目录 时序预测 | MATLAB实现LSTM时间序列未来多步预测-递归预测基本介绍程序设计参考资料 基本介绍 MATLAB实现LSTM时间序列未来多步预测-递归预测。LSTM是一种含有LSTM区块(blocks)或其他的一种类神经网络,文献或其他资料中LSTM区块可能被描述成智能网络单元,因为

C++入门01

1、.h和.cpp 源文件 (.cpp)源文件是C++程序的实际实现代码文件,其中包含了具体的函数和类的定义、实现以及其他相关的代码。主要特点如下:实现代码: 源文件中包含了函数、类的具体实现代码,用于实现程序的功能。编译单元: 源文件通常是一个编译单元,即单独编译的基本单位。每个源文件都会经过编译器的处理,生成对应的目标文件。包含头文件: 源文件可以通过#include指令引入头文件,以使

vue项目集成CanvasEditor实现Word在线编辑器

CanvasEditor实现Word在线编辑器 官网文档:https://hufe.club/canvas-editor-docs/guide/schema.html 源码地址:https://github.com/Hufe921/canvas-editor 前提声明: 由于CanvasEditor目前不支持vue、react 等框架开箱即用版,所以需要我们去Git下载源码,拿到其中两个主