C++|运算符重载(3)|日期类的计算

简介: C++|运算符重载(3)|日期类的计算

前面准备

日期的运算需要考虑到每个月天数不一样,平年闰年2月天数也不一样,因此需要先写两个函数

1.判断year是否为闰年

2.返回当前年月的天数

代码如下

bool leap(int year)
{
  //不是百年4年一闰
  if (year % 100 != 0 && year % 4 == 0)
    return true;
  if (year % 100 == 0 && year % 400 == 0)
    return true;
  return false;
}
 
 
//每月多少天
int MonthDay(int year, int month)
{
  int arr[13] = { 0,31,29,31,30,31,30,31,31,30,31,30,31 };
  if (month == 2 && !leap(year))
    return arr[month]-1;
  else 
    return arr[month];
}

实现功能:

-(分为Date类和Date类相减,Date类和int 相减),+,+=,-=,==,!=,<,<=,>,>=,++,--。

-运算符

这里实现Date类和Date类相减,Date类和int 相减

Date类和int 相减

Date类和int相减,也就是要知道当前日期下几天前的日期,结果是要日期,因此函数的返回类型就是Date,另外-运算符是一个双目运算符,因此运算符重载函数的形参列表只能写一个形参(成员函数有一个默认的this指针),代码如下:

 

Date operator-(int day)
{
  day -= Day;
  Month--;
  while (day > MonthDay(Year, Day))
  {
    day -= MonthDay(Year, Month);
    Month--;
    if (Month < 1)
    {
      Year--;
      Month = 12;
    }
  }
  Day = MonthDay(Year, Month) - day;
  return *this;
}

Date类和Date类相减

这里我用的是分别算出date1和date2到1年1月1日的天数,然后相减得到两个日期相差的天数,结果是一个天数,那么函数类型就是int,先写日期到1年1月1日天数的函数,代码如下:

 

int number1()
{
  int year = 1,month = 1;
  int thisday = 0;
  while (year < Year)
  {
    thisday += (leap(year)) ? 366 : 365;
    year++;
  }
 
  while (month < Month)
  {
    thisday += MonthDay(Year, month);
    month++;
  }
  thisday += Day;
  return thisday;
}

接下来是Date类和Date相减的函数

1. int operator-(Date date)
2. {
3.  return this->number1()-date.number1();
4. }

+运算符

这里实现的是,Date类和int相加的结果,也就是,在当前日期下,几天后是几月几号,从这就可以看出运算后的结果是一个Date类,因此函数的类型也就是Date.在函数体刚开始我判断了day是否小于0,小于0也就是几天前是几月几号,直接调用-运算符重载函数就可以。

Date operator+(int day)
{
  if (day < 0)
  {
    return operator-(day);
    
  }
    day += Day;
  while (day > MonthDay(Year, Month))
  {
    day -= MonthDay(Year, Month);
    Month++;
    if (Month > 12)
    {
      Year++;
      Month = 1;
    }
  }
  Day = day;
 
  return *this;
}

+=,-=运算符

+=运算符可以直接调用+运算符重载函数实现

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

同理-=也是同样的道理

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

==,!=运算符

==是判断左右两边的表达式是否相等,Date要相等,就得年相等,月相等,日相等,同时满足,才返回true,代码如下,

bool operator==(Date date)
{
  if(Year == date.Year&&Month==date.Month&&Day==date.Day)return true;
  return false;
}

!=就容易判断了,==的反面就是!= ;

bool operator!=(Date date)
{
  if (*this == date)return false;
  return true;
}

>,>=运算符

在我们的认知里,先出现的日期会大,因此比较的时候数值越小,日期越大,逐一比较就行,具体代码如下:

bool operator>(Date date)
{
  if (Year < date.Year)
    return true;
  else
  {
    if (Month < date.Month)
      return true;
    else
    {
      if (Day < date.Day)
        return true;
      else false;
    }
  }
}

>=,就是既满足大于也满足相等,满足其一即可,具体代码如下

bool operator>=(Date date)
{
  if (*this > date||*this == date)
    return true;
  else
    return false;
}

<,<=运算符

与前面同理,在写<的时候,可以借助前面的>=的函数,>=的反面就是<,具体代码如下:

 

bool operator<(Date date)
{
  if (*this >= date )return false;
  return true;
}

<=的反面是>,因此可以借助>,来实现<=的重载

bool operator<=(Date date)
{
  if (*this > date)return false;
  else return true;
 
}

++,--运算符

自增,自减运算符就是对自身进行加1,减1的操作,返回自身,需要注意的就是进位,例如:若是Month加到13,那么应该,Year++;Month=1;具体代码如下:

Date operator++()
{
  Day++;
  if (Day > MonthDay(Year, Month))
  {
    Month++;
    if (Month > 12)
    {
      Year++;
      Month = 1;
    }
    Day = 1;
  }
  
  return *this;
}
 
Date operator--()
{
  if (Day == 1)
  {
    Month--;
    if (Month == 0)
    {
      Year--;
      Month = 12;
    }
    Day = MonthDay(Year, Month);
    return *this;
  }
}

功能测试

 
int main()
{
 
 
  cout << "相减:---------------------" << endl;
  Date date2(2024, 1, 9);
  cout << "date2:";
  date2.DatePrint();
  Date date3 = date2 - 679;
  cout << "date2-679=" ;
  date3.DatePrint();
 
  cout <<endl<< "相差:------------------------------------" << endl;
  cout << "date4:";
  Date date4(2023, 9, 9);
  date4.DatePrint();
  cout << "date5:";
  Date date5(2009, 4, 5);
  date5.DatePrint();
  cout << "date5-date4="<< date5 - date4 << endl;
 
  cout << endl<<"相加:----------------------------------" << endl;
 
  Date date(2024, 1, 9);
  cout << "date:";
  date.DatePrint();
  Date date1 = date + 2000;
  cout << "date+2000=";
  date1.DatePrint();
 
  cout <<endl<< "+= ---------------------------" << endl;
  Date date6(2024, 1, 9);
  cout << "date6:";
  date6.DatePrint();
  date6 += 138;
  cout << "date6 += 138:";
  date6.DatePrint();
 
  cout << endl<<"-=-----------------------------" << endl;
  Date date7(2024, 1, 9);
  cout << "date7:";
  date7.DatePrint();
  date7 -= 138;
  cout << "date7-=138:";
  date7.DatePrint();
 
 
 
  cout << endl<<"==------------------------------" << endl;
  Date date8(2024, 1, 9);
  cout << "date8:";
  date8.DatePrint();
  Date date9(2024, 1, 9);
  cout << "date9:";
  date9.DatePrint();
  cout << "date8==date9:" << (date8 == date9) << endl;
 
  cout << endl<<"!=------------------------------------" << endl;
  Date date10(2024, 1, 9);
  cout << "date10:";
  date10.DatePrint();
  Date date11(2024, 1, 10);
  cout << "date11:";
  date11.DatePrint();
  cout << "date10!=date11:" << (date10 != date11) << endl;
 
  cout << endl<<">----------------------------------------" << endl;
  Date date12(2024, 1, 9);
  cout << "date12:";
  date12.DatePrint();
 
  Date date13(2024, 1, 10);
  cout << "date13:";
  date13.DatePrint();
  cout << "date10>date11:" << (date10 > date11) << endl;
 
  cout <<endl<< "++------------------------------" << endl;
  Date date14(2024, 1, 31);
  cout << "date14:";
  date14.DatePrint();
  ++date14;
  cout << "++date14:";
  date14.DatePrint();
 
 
  cout <<endl<< "--              --------------------------" << endl;
  Date date15(2024, 1, 1);
  cout << "date15:";
  date15.DatePrint();
  --date15;
  cout << "--date15:";
  date15.DatePrint();
 
 
 
 
  return 0;
 
}

结果:

完整代码

Date.h

#include<iostream>
#include<stdbool.h>
using namespace std;
bool leap(int year);
int MonthDay(int year, int month);
 
//闰年
 
class Date
{
private:
  int Year;
  int Month;
  int Day;
public:
  //构造函数
  Date();
  Date(int year,int month,int day);
  void DatePrint();
  //加法
  Date operator+(int day);
  //减法
  Date operator-(int day);
  //当前日期到1年1月1日的天数
  int number1();
  //相差
  int operator-(Date date);
  //判断相等
  bool operator==(Date date);
  //判断不相等
  bool operator!=(Date date);
  //实现-=
  Date operator-=(int day);
  //实现+=
  Date operator+=(int day);
  //判断>
  bool operator>(Date date);
  //判断<
  bool operator<(Date date);
  //判断>=
  bool operator<=(Date date);
  //判断>=
  bool operator>=(Date date);
  //++
  Date operator++();
  //--
  Date operator--();
};
 
 

fun.cpp

#include"Date.h"
bool leap(int year)
{
  //不是百年4年一闰
  if (year % 100 != 0 && year % 4 == 0)
    return true;
  if (year % 100 == 0 && year % 400 == 0)
    return true;
  return false;
}
 
 
//每月多少天
int MonthDay(int year, int month)
{
  int arr[13] = { 0,31,29,31,30,31,30,31,31,30,31,30,31 };
  if (month == 2 && !leap(year))
    return arr[month]-1;
  else 
    return arr[month];
}
 
Date::Date(int year, int month, int day)
{
  Year = year;
  Month = month;
  Day = day;
}
void Date::DatePrint()
{
  cout << Year << "/" << Month << "/" << Day << endl;
}
 
Date::Date()
{
  Year = 1;
  Month = 1;
  Day = 1;
}
 
 
 
Date Date::operator-(int day)
{
  day -= Day;
  Month--;
  while (day > MonthDay(Year, Day))
  {
    day -= MonthDay(Year, Month);
    Month--;
    if (Month < 1)
    {
      Year--;
      Month = 12;
    }
  }
  Day = MonthDay(Year, Month) - day;
  return *this;
}
 
Date Date::operator+(int day)
{
  if (day < 0)
  {
    return operator-(day);
    
  }
    day += Day;
  while (day > MonthDay(Year, Month))
  {
    day -= MonthDay(Year, Month);
    Month++;
    if (Month > 12)
    {
      Year++;
      Month = 1;
    }
  }
  Day = day;
 
  return *this;
}
 
int Date:: number1()
{
  int year = 1,month = 1;
  int thisday = 0;
  while (year < Year)
  {
    thisday += (leap(year)) ? 366 : 365;
    year++;
  }
 
  while (month < Month)
  {
    thisday += MonthDay(Year, month);
    month++;
  }
  thisday += Day;
  return thisday;
}
 
int Date::operator-(Date date)
{
  return this->number1()-date.number1();
}
 
 
bool Date::operator==(Date date)
{
  if(Year == date.Year&&Month==date.Month&&Day==date.Day)return true;
  return false;
}
 
bool Date::operator!=(Date date)
{
  if (*this == date)return false;
  return true;
}
 
 
Date Date::operator-=(int day)
{
  *this = *this - day;
  return *this;
}
 
Date Date::operator+=(int day)
{
  *this = *this + day;
  return *this;
}
 
bool Date::operator>(Date date)
{
  if (Year < date.Year)
    return true;
  else
  {
    if (Month < date.Month)
      return true;
    else
    {
      if (Day < date.Day)
        return true;
      else false;
    }
  }
}
bool Date::operator>=(Date date)
{
  if (*this > date||*this == date)
    return true;
  else
    return false;
}
 
bool Date::operator<(Date date)
{
  if (*this >= date )return false;
  return true;
}
 
bool Date::operator<=(Date date)
{
  if (*this > date)return false;
  else return true;
 
}
 
 
 
Date Date::operator++()
{
  Day++;
  if (Day > MonthDay(Year, Month))
  {
    Month++;
    if (Month > 12)
    {
      Year++;
      Month = 1;
    }
    Day = 1;
  }
  
  return *this;
}
 
Date Date::operator--()
{
  if (Day == 1)
  {
    Month--;
    if (Month == 0)
    {
      Year--;
      Month = 12;
    }
    Day = MonthDay(Year, Month);
    return *this;
  }
}

test.cpp

#include"Date.h"
int main()
{
 
 
  cout << "相减:---------------------" << endl;
  Date date2(2024, 1, 9);
  cout << "date2:";
  date2.DatePrint();
  Date date3 = date2 - 679;
  cout << "date2-679=" ;
  date3.DatePrint();
 
  cout <<endl<< "相差:------------------------------------" << endl;
  cout << "date4:";
  Date date4(2023, 9, 9);
  date4.DatePrint();
  cout << "date5:";
  Date date5(2009, 4, 5);
  date5.DatePrint();
  cout << "date5-date4="<< date5 - date4 << endl;
 
  cout << endl<<"相加:----------------------------------" << endl;
 
  Date date(2024, 1, 9);
  cout << "date:";
  date.DatePrint();
  Date date1 = date + 2000;
  cout << "date+2000=";
  date1.DatePrint();
 
  cout <<endl<< "+= ---------------------------" << endl;
  Date date6(2024, 1, 9);
  cout << "date6:";
  date6.DatePrint();
  date6 += 138;
  cout << "date6 += 138:";
  date6.DatePrint();
 
  cout << endl<<"-=-----------------------------" << endl;
  Date date7(2024, 1, 9);
  cout << "date7:";
  date7.DatePrint();
  date7 -= 138;
  cout << "date7-=138:";
  date7.DatePrint();
 
 
 
  cout << endl<<"==------------------------------" << endl;
  Date date8(2024, 1, 9);
  cout << "date8:";
  date8.DatePrint();
  Date date9(2024, 1, 9);
  cout << "date9:";
  date9.DatePrint();
  cout << "date8==date9:" << (date8 == date9) << endl;
 
  cout << endl<<"!=------------------------------------" << endl;
  Date date10(2024, 1, 9);
  cout << "date10:";
  date10.DatePrint();
  Date date11(2024, 1, 10);
  cout << "date11:";
  date11.DatePrint();
  cout << "date10!=date11:" << (date10 != date11) << endl;
 
  cout << endl<<">----------------------------------------" << endl;
  Date date12(2024, 1, 9);
  cout << "date12:";
  date12.DatePrint();
 
  Date date13(2024, 1, 10);
  cout << "date13:";
  date13.DatePrint();
  cout << "date10>date11:" << (date10 > date11) << endl;
 
  cout <<endl<< "++------------------------------" << endl;
  Date date14(2024, 1, 31);
  cout << "date14:";
  date14.DatePrint();
  ++date14;
  cout << "++date14:";
  date14.DatePrint();
 
 
  cout <<endl<< "--              --------------------------" << endl;
  Date date15(2024, 1, 1);
  cout << "date15:";
  date15.DatePrint();
  --date15;
  cout << "--date15:";
  date15.DatePrint();
 
 
 
 
  return 0;
 
}


相关文章
|
4月前
|
人工智能 机器人 编译器
c++模板初阶----函数模板与类模板
class 类模板名private://类内成员声明class Apublic:A(T val):a(val){}private:T a;return 0;运行结果:注意:类模板中的成员函数若是放在类外定义时,需要加模板参数列表。return 0;
94 0
|
4月前
|
存储 编译器 程序员
c++的类(附含explicit关键字,友元,内部类)
本文介绍了C++中类的核心概念与用法,涵盖封装、继承、多态三大特性。重点讲解了类的定义(`class`与`struct`)、访问限定符(`private`、`public`、`protected`)、类的作用域及成员函数的声明与定义分离。同时深入探讨了类的大小计算、`this`指针、默认成员函数(构造函数、析构函数、拷贝构造、赋值重载)以及运算符重载等内容。 文章还详细分析了`explicit`关键字的作用、静态成员(变量与函数)、友元(友元函数与友元类)的概念及其使用场景,并简要介绍了内部类的特性。
170 0
|
6月前
|
编译器 C++ 容器
【c++11】c++11新特性(上)(列表初始化、右值引用和移动语义、类的新默认成员函数、lambda表达式)
C++11为C++带来了革命性变化,引入了列表初始化、右值引用、移动语义、类的新默认成员函数和lambda表达式等特性。列表初始化统一了对象初始化方式,initializer_list简化了容器多元素初始化;右值引用和移动语义优化了资源管理,减少拷贝开销;类新增移动构造和移动赋值函数提升性能;lambda表达式提供匿名函数对象,增强代码简洁性和灵活性。这些特性共同推动了现代C++编程的发展,提升了开发效率与程序性能。
183 12
|
7月前
|
设计模式 安全 C++
【C++进阶】特殊类设计 && 单例模式
通过对特殊类设计和单例模式的深入探讨,我们可以更好地设计和实现复杂的C++程序。特殊类设计提高了代码的安全性和可维护性,而单例模式则确保类的唯一实例性和全局访问性。理解并掌握这些高级设计技巧,对于提升C++编程水平至关重要。
131 16
|
7月前
|
编译器 C++
类和对象(中 )C++
本文详细讲解了C++中的默认成员函数,包括构造函数、析构函数、拷贝构造函数、赋值运算符重载和取地址运算符重载等内容。重点分析了各函数的特点、使用场景及相互关系,如构造函数的主要任务是初始化对象,而非创建空间;析构函数用于清理资源;拷贝构造与赋值运算符的区别在于前者用于创建新对象,后者用于已存在的对象赋值。同时,文章还探讨了运算符重载的规则及其应用场景,并通过实例加深理解。最后强调,若类中存在资源管理,需显式定义拷贝构造和赋值运算符以避免浅拷贝问题。
|
7月前
|
存储 编译器 C++
类和对象(上)(C++)
本篇内容主要讲解了C++中类的相关知识,包括类的定义、实例化及this指针的作用。详细说明了类的定义格式、成员函数默认为inline、访问限定符(public、protected、private)的使用规则,以及class与struct的区别。同时分析了类实例化的概念,对象大小的计算规则和内存对齐原则。最后介绍了this指针的工作机制,解释了成员函数如何通过隐含的this指针区分不同对象的数据。这些知识点帮助我们更好地理解C++中类的封装性和对象的实现原理。
|
7月前
|
编译器 C++
类和对象(下)C++
本内容主要讲解C++中的初始化列表、类型转换、静态成员、友元、内部类、匿名对象及对象拷贝时的编译器优化。初始化列表用于成员变量定义初始化,尤其对引用、const及无默认构造函数的类类型变量至关重要。类型转换中,`explicit`可禁用隐式转换。静态成员属类而非对象,受访问限定符约束。内部类是独立类,可增强封装性。匿名对象生命周期短,常用于临时场景。编译器会优化对象拷贝以提高效率。最后,鼓励大家通过重复练习提升技能!
|
8月前
|
编译器 C++ 开发者
【C++篇】深度解析类与对象(下)
在上一篇博客中,我们学习了C++的基础类与对象概念,包括类的定义、对象的使用和构造函数的作用。在这一篇,我们将深入探讨C++类的一些重要特性,如构造函数的高级用法、类型转换、static成员、友元、内部类、匿名对象,以及对象拷贝优化等。这些内容可以帮助你更好地理解和应用面向对象编程的核心理念,提升代码的健壮性、灵活性和可维护性。
|
8月前
|
编译器 C语言 C++
类和对象的简述(c++篇)
类和对象的简述(c++篇)
|
7月前
|
安全 C++
【c++】继承(继承的定义格式、赋值兼容转换、多继承、派生类默认成员函数规则、继承与友元、继承与静态成员)
本文深入探讨了C++中的继承机制,作为面向对象编程(OOP)的核心特性之一。继承通过允许派生类扩展基类的属性和方法,极大促进了代码复用,增强了代码的可维护性和可扩展性。文章详细介绍了继承的基本概念、定义格式、继承方式(public、protected、private)、赋值兼容转换、作用域问题、默认成员函数规则、继承与友元、静态成员、多继承及菱形继承问题,并对比了继承与组合的优缺点。最后总结指出,虽然继承提高了代码灵活性和复用率,但也带来了耦合度高的问题,建议在“has-a”和“is-a”关系同时存在时优先使用组合。
356 6