C++实现日期类(Date)

所属分类: 软件编程 / C 语言 阅读数: 45
收藏 0 赞 0 分享

本文实例为大家分享了C++实现日期类的具体代码,供大家参考,具体内容如下

#include<iostream>
#include<stdlib.h>
using namespace std;
class Date
{
public:
 //构造函数
 Date(int year = 1900, int month = 1, int day = 1)
 :_year(year)
 , _month(month)
 , _day(day)
 {
 if (!IsInvalidDate(_year, _month, _day))
 {
  _year = 1900;
  _month = 1;
  _day = 1;
 }
 }
 //拷贝函数
 Date(const Date& d)
 : _year(d._year)
 , _month(d._month)
 , _day(d._day)
 {}
 
 //析构函数
 ~Date()
 {}
 
 //判断是不是闰年
 bool IsLeapYear(int year)
 {
  if ((year % 400 == 0) ||
  ((year % 4 == 0) && (year % 100 != 0)) )
  {
  return true;
  }
  return false;
 }
 //判断是不是合法日期
 bool IsInvalidDate(int year, int month, int day)
 {
  if ((year < 1) ||
  (month < 0 || month >12) ||
  (day < 0 || day > YearsOfMonth(year, month)))
  {
  return false;
  }
  return true;
 }
 //判断当前月份多少天
 int YearsOfMonth(int year, int month)
 {
  int day;
  int days[13] = {0,31,28,31,30,31,30,31,31,30,31,30,31};
  day = days[month];
  if (month == 2 && IsLeapYear(year))
  {
  day += 1;
  }
  return day;
 }
 //修正日期
 Date ToCorrect(Date &d)
 {
  while (d._day > YearsOfMonth(d._year, d._month) || d._day <= 0)
  {
  if(d._day <= 0)
  {
   d._day += YearsOfMonth(d._year,( d._month - 1));
   if (d._month == 1)
   {
   d._month = 12;
   d._year--;
   }
   else
   {
   d._month--;
   }
  }
  else
  {
   d._day -= YearsOfMonth(d._year, d._month);
   if (d._month == 12)
   {
   d._year++;
   d._month = 1;
   }
   else
   {
   d._month++;
   }
  }
  }
  return d;
 }
 // 当前日期days天后是什么日期? 
 Date operator+(int days)
 {
  Date tmp(*this);
  tmp._day += days;
  ToCorrect(tmp);
  return tmp;
 }
 
 // 当前日期days天前是什么日期? 
 Date operator-(int days)
 {
  Date tmp(*this);
  tmp._day -= days;
  ToCorrect(tmp);
  return tmp;
 }
 
 // 日期比大小 
 bool operator>(const Date& d)
 {
 return ( _year > d._year || 
  (_year == d._year && _month > d._month) ||
  (_year == d._year && _month == d._month && _day > d._day));
 }
 bool operator<(const Date& d)
 {
 return (_year < d._year ||
  (_year == d._year && _month < d._month) ||
  (_year == d._year && _month == d._month && _day < d._day));
 }
 bool operator==(const Date& d)
 {
 return ((_year == d._year) && (_month == d._month) && (_day == d._day));
 }
 bool operator!=(const Date& d)
 {
 return !(*this == d);
 }
 bool operator>=(const Date &d)
 {
 return !(*this<d);
 }
 bool operator<=(const Date &d)
 {
 return !(*this>d);
 }
 
 // 重载取地址符号 
 Date* operator&()
 {
 
 }
 
 // 前置++ 
 Date& operator++()
 {
 (*this)++;
 return *this;
 }
 
 // 后置++ 
 Date operator++(int)//通过返回值和传参区别前置和后置++
 {
 Date tmp(*this);
 (*this) = (*this) + 1;
 return tmp;
 }
 
 // 前置-- 
 Date& operator--()
 {
 (*this)--;
 return *this;
 }
 
 // 后置-- 
 Date operator--(int)
 {
 Date tmp(*this);
 (*this)--;
 return tmp;
 }
 void Display()
 {
 cout << _year << "-" << _month << "-" << _day << endl;
 }
private:
 int _year;
 int _month;
 int _day;
};
 
int main()
{
 Date d(2018, 9, 9);
 d.Display();
 Date d1 = d + 50;
 d1.Display();
 d1 =d1 - 50;
 d1.Display();
 
 cout << "------"<<endl;
 cout << "前置++" << endl;
 d1.Display();
 (++d1).Display();
 d1.Display();
 cout << "后置++" << endl;
 d1.Display();
 (d1++).Display();
 d1.Display();
 cout << "------" << endl;
 
 
 system("pause");
 return 0;
}

以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持脚本之家。

更多精彩内容其他人还在看

C语言数组入门之数组的声明与二维数组的模拟

这篇文章主要介绍了C语言数组入门之数组的声明与二维数组的模拟,数组学习的同时也要相应理解C语言指针的作用,需要的朋友可以参考下
收藏 0 赞 0 分享

C语言中变量与其内存地址对应的入门知识简单讲解

这篇文章主要介绍了C语言中变量与其内存地址对应的入门知识简单讲解,同时这也是掌握指针部分知识的基础,需要的朋友可以参考下
收藏 0 赞 0 分享

讲解C语言编程中指针赋值的入门实例

这篇文章主要介绍了讲解C语言编程中指针赋值的入门实例,通过const int i与int *const pi这样两个例子来分析指针的赋值和地址指向,需要的朋友可以参考下
收藏 0 赞 0 分享

C语言中的结构体的入门学习教程

这篇文章主要介绍了C语言中的结构体的入门学习教程,以struct语句定义的结构体是C语言编程中的重要基础,需要的朋友可以参考下
收藏 0 赞 0 分享

C语言编程入门之程序头文件的简要解析

这篇文章主要介绍了C语言编程入门之程序头文件的简要解析,包括头文件重复包含问题等方面的说明,需要的朋友可以参考下
收藏 0 赞 0 分享

C语言编程中的联合体union入门学习教程

这篇文章主要介绍了C语言编程中的联合体union入门学习教程,也是C语言入门学习中的基础知识,需要的朋友可以参考下
收藏 0 赞 0 分享

C语言中数组作为函数的参数以及返回值的使用简单入门

这篇文章主要介绍了C语言中数组作为函数的参数以及返回值的使用简单入门,这里以一维数组作为基本条件进行例子讲解,需要的朋友可以参考下
收藏 0 赞 0 分享

MySQL的内存表的基础学习教程

这篇文章主要介绍了MySQL的内存表的基础学习教程,包括内存表的创建以及使用限制等等,需要的朋友可以参考下
收藏 0 赞 0 分享

C++中头文件的概念与基本编写方法

这篇文章主要介绍了C++中头文件的概念与基本编写方法,是C++入门学习中的基础知识,需要的朋友可以参考下
收藏 0 赞 0 分享

jQuery移动页面开发中主题按钮的设计示例

这篇文章主要介绍了jQuery移动页面开发中主题按钮的设计示例,jQuery是当今最具人气的JavaScript开发类库,需要的朋友可以参考下
收藏 0 赞 0 分享
查看更多