C++实现日期类(Date)

本文实例为大家分享了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;
}

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

(0)

相关推荐

  • 详解C++句柄类

    上一篇文件介绍了关于C++代理类的使用场景和实现方法,但是代理类存在一定的缺陷,就是每个代理类会创建一个新的对象,无法避免一些不必要的内存拷贝,本篇文章引入句柄类,在保持代理类多态性的同时,还可以避免进行不不要的对象复制. 我们先来看一个简易的字符串封装类:MyString,为了方便查看代码,将函数的声明和实现放到了一起. class MyString { public: // 默认构造函数 MyString() { std::cout << "MyString()" &l

  • C++类的分离式写法介绍示例

    介绍 类的分离式写法,使得代码更加规范,增强了阅读性. 分离式写法的规则: 1.类的变量:写在类的里面 2.成员函数:类中写函数的声明,函数的定义写在类体外. 3.写在类外函数定义时,类名前加限定(Object: :),其中,::理解为表示范围的符号. 代码演示 头文件:Object.h #ifndef _OBJECT_H_ #define _OBJECT_H_ class Student { private: char name[32]; int age; public: void SetNa

  • C/C++中的sizeof运算符和size_t类型的详解

    sizeof的作用 sizeof是c的运算符之一,用于获取操作数被分配的内存空间,以字节单位表示. 这里指的操作数,可以是变量,也可以是数据类型,如int,float等.所以就可以通过它来获取本地c库定义的基本类型的范围. sizeof的使用 1.对于一般变量,形式2种:sizeof a 或 sizeof(a); 2.对于数据类型,必须使用带括号的方式,如sizeof(int). size_t的说明 size_t是标准C库中定义的,应为unsigned int,在64位系统中为 long uns

  • 关于C++友元类的实现讲解

    C++中的友元既可以实现友元函数,也可以实现友元类,也就是说一个类也可以作为另外一个类的友元.当作为一个类的友元时,它的所有成员函数都是另一个类的友元函数,都可以访问另一个类的私有或者公有成员. 请看实例: #include <iostream> #include <cstring> using namespace std ; //声明教师类 class Techer ; //学生类 class Student { private: string name ; int age ;

  • C++11并发编程关于原子操作atomic的代码示例

    一:概述 项目中经常用遇到多线程操作共享数据问题,常用的处理方式是对共享数据进行加锁,如果多线程操作共享变量也同样采用这种方式. 为什么要对共享变量加锁或使用原子操作?如两个线程操作同一变量过程中,一个线程执行过程中可能被内核临时挂起,这就是线程切换,当内核再次切换到该线程时,之前的数据可能已被修改,不能保证原子操作. C++11提供了个原子的类和方法atomic,保证了多线程对变量原子性操作,相比加锁机制mutex.lock(),mutex.unlock(),性能有几倍的提升. 所需头文件<a

  • C++ 读取文件内容到指定类型的变量方法

    如下所示: #include <iostream> #include <fstream> #include <sstream> #include <string> using namespace std; int main(){ cout << "input the file name: "; string file_name; cin >> file_name; cout << endl; // if

  • 关于C++内部类的介绍与使用示例

    介绍 1.把一个类定义在另一个类的内部,称里面的类为内部类. 例如: class A { public: class B { public: int x; int y; }; }; 类B即为内部类. 2.内部类和外部类相互没有特权,即外部类无法自由访问内部类,内部类也无法自由访问外部类. a.他们不是朋友关系 b.他们不是父子关系 内部类的使用 #include <stdio.h> class A { public: class B { public: void test() { printf

  • C++关于构造函数可向父类或者本类传参的讲解

    前面我们学习了C++使用初始化列表的方式来初始化字段的方法: https://www.jb51.net/article/153032.htm 这一节的原理和前面的差不多. 在C++的构造函数中,子类继承父类,那么,在创建一个子类成员时,可以同时向父类或者子类的构造函数进行传参,实现方法如下: 写一个例子:mul_argc.c #include <iostream> #include <cstring> using namespace std ; //英雄联盟类 class Hero

  • 解决易语言转换到C++ 自定义数据类型

    自定义数据类型如下 .版本 2 .数据类型 数据 .成员 坐标, 坐标_数据类型 .数据类型 坐标_数据类型 .成员 x, 小数型 .成员 z, 小数型 .成员 y, 小数型 这里的自定义数据类型下的"数据"类型下的"坐标"成员引用自定义数据类型"坐标_数据类型" 子程序如下 .版本 2 .子程序 自己数据 .参数 返回数据, 数据, 参考 返回数据.坐标.x = 1 返回数据.坐标.z = 2 返回数据.坐标.y = 3 这里的子程序内的参数&

  • C++标准模板库string类的介绍与使用讲解

    介绍 c++中字符串string对象属于一个类,内置了很多实用的成员函数,操作简单,方便更直观. 命名空间为std,所属头文件<string> 注意:不是<string.h>. 跟进代码会发现string其实只是basic_string模板类的一个typedef. 赋值 //方法1 string str1 = "woniu201"; //方法2 char* p = "woniu201"; string str2 = p; 遍历 //方法1 使

随机推荐