【C++】实现日期类相关接口(三)

简介: 【C++】实现日期类相关接口

【C++】实现日期类相关接口(二)https://developer.aliyun.com/article/1617311


7.2 Date.cpp

#include"Date.h"
Date::Date(int year, int month, int day)
{
  _year = year;
  _month = month;
  _day = day;
}
bool Date::operator<(const Date& d)
{
  if (_year < d._year)
  {
    return true;
  }
  else if (_year == d._year)
  {
    if (_month < d._month)
    {
      return true;
    }
    else if (_month == d._month)
    {
      if (_day < d._day)
      {
        return true;
      }
    }
  }
  return false;
}
// d1 <= d2
bool Date::operator<=(const Date& d)
{
  return *this < d || *this == d;
}
bool Date::operator>(const Date& d)
{
  return !(*this <= d);
}
bool Date::operator>=(const Date& d)
{
  return !(*this < d);
}
bool Date::operator==(const Date& d)
{
  return _year == d._year
    && _month == d._month
    && _day == d._day;
}
bool Date::operator!=(const Date& d)
{
  return !(*this == d);
}
// d1 += 10
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);
  Date tmp = *this; // 
  tmp += day;
  return tmp;
}
// d1 + 10
//Date Date::operator+(int day)
//{
//  //Date tmp(*this);
//  Date tmp = *this; // 
//
//  tmp._day += day;
//  while (tmp._day > GetMonthDay(tmp._year, tmp._month))
//  {
//    tmp._day -= GetMonthDay(tmp._year, tmp._month);
//    ++tmp._month;
//    if (tmp._month == 13)
//    {
//      ++tmp._year;
//      tmp._month = 1;
//    }
//  }
//
//  return tmp;
//}
//
 d1 += 100
//Date& Date::operator+=(int day)
//{
//  *this = *this + day;
//
//  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;
}
// ++d ->d.operator++()
Date& Date::operator++()
{
  *this += 1;
  return *this;
}
// d++ ->d.operator++(0)
Date Date::operator++(int)
{
  Date tmp = *this;
  *this += 1;
  return tmp;
}
// d1 - d2
int Date::operator-(const Date& d)
{
  int flag = 1;
  Date max = *this;
  Date min = d;
  if (*this < d)
  {
    int flag = -1;
    max = d;
    min = *this;
  }
  int n = 0;
  while (min != max)
  {
    ++min;
    ++n;
  }
  return n * flag;
}

7.3 test.cpp

#define _CRT_SECURE_NO_WARNINGS 1
#include "Date.h"
int main()
{
  Date d1(2024, 1, 29);
  Date d2 = d1 + 20;
  d2.Print();
  d1.Print();
  d2 -= 20;
  d2.Print();
  d1 += 30000;
  d1.Print();
  ++d1;
  d1.operator++();
  d1.Print();
  d1++;
  d1.operator++(10);
  d1.Print();
  /*bool ret = false;
  if (ret)
  {
    d1.Print();
  }*/
  Date d4(2024, 1, 29);
  Date d5(2024, 8, 1);
  cout << d5 - d4 << endl;
  return 0;
}

以上就是本篇文章的所有内容,在此感谢大家的观看!这里是店小二呀C++笔记,希望对你在学习C++语言旅途中有所帮助!

相关文章
|
1天前
|
存储 C++
【C++篇】C++类和对象实践篇——从零带你实现日期类的超详细指南
【C++篇】C++类和对象实践篇——从零带你实现日期类的超详细指南
10 2
【C++篇】C++类和对象实践篇——从零带你实现日期类的超详细指南
|
1天前
|
安全 C语言 C++
【C++篇】探寻C++ STL之美:从string类的基础到高级操作的全面解析
【C++篇】探寻C++ STL之美:从string类的基础到高级操作的全面解析
18 4
|
1天前
|
存储 编译器 数据安全/隐私保护
【C++篇】C++类与对象深度解析(四):初始化列表、类型转换与static成员详解2
【C++篇】C++类与对象深度解析(四):初始化列表、类型转换与static成员详解
12 3
|
1天前
|
编译器 C++
【C++篇】C++类与对象深度解析(四):初始化列表、类型转换与static成员详解1
【C++篇】C++类与对象深度解析(四):初始化列表、类型转换与static成员详解
14 3
|
1天前
|
存储 编译器 程序员
【C++篇】手撕 C++ string 类:从零实现到深入剖析的模拟之路
【C++篇】手撕 C++ string 类:从零实现到深入剖析的模拟之路
17 2
|
1天前
|
安全 编译器 程序员
【C++篇】C++类与对象深度解析(六):全面剖析拷贝省略、RVO、NRVO优化策略
【C++篇】C++类与对象深度解析(六):全面剖析拷贝省略、RVO、NRVO优化策略
9 2
|
1天前
|
存储 设计模式 编译器
【C++篇】C++类与对象深度解析(五):友元机制、内部类与匿名对象的高级应用
【C++篇】C++类与对象深度解析(五):友元机制、内部类与匿名对象的高级应用
10 2
|
1天前
|
存储 编译器 C语言
C++类与对象深度解析(一):从抽象到实践的全面入门指南
C++类与对象深度解析(一):从抽象到实践的全面入门指南
15 8
|
1天前
|
安全 编译器 C++
【C++篇】C++类与对象深度解析(三):类的默认成员函数详解
【C++篇】C++类与对象深度解析(三):类的默认成员函数详解
9 3
|
1天前
|
C++
【C++】深入探索类和对象:初始化列表及其static成员与友元(二)
【C++】深入探索类和对象:初始化列表及其static成员与友元