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

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

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


五、日期减日期

需要使用持续借位原则,如果天数为0,需要得到上月的天数

第一种方式:不断++直到等于大的那个年份,这里需要日期加日期接口的技术支撑

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;//这里会调用operato++()
    ++n;//operator++()
  }
  return n * flag;
}
  • 优点:方便实现
  • 缺点:效率低

第二个方法:通过该变量记录两个年份修饰到1月1日,也就是都修饰到1月1日之间还差多少天,再计算两个年之间有多少个年,如果是平年+365,闰年+366

int Date::operator-(const Date& d) const
{
  //不知道哪个操作数大,先假设
  Date max = *this;
  Date min = d;
  int flag = 1;
  if (*this < d)//假设错了就认错
  {
    Date max = d;
    Date min = *this;
    int flag = -1;//用来标记
  }
  int count =0;
  //大的减到1月1日  count++
  while (!(max._day == 1 && max._month == 1))
  {
    --max;
    ++count;
  }
  //小的减到1月1日  count--
  while (!(min._day == 1 && min._month == 1))
  {
    --min;
    --count;
  }
  //都减到1月1日了  算差多少年
  while (min._year != max._year)
  {
    if (is_leapyear(min._year))
      count += 366;
    else
      count += 365;
    ++min._year;
  }
  return flag * count;
}

六、流插入和流提取运算符重载

out和cin的本质是输入和输出流对象,对于<<和>>用于重载的运算符,从图可以得,cout属于ostream类,cin属于istream类,可以自动识别类型

对于我们可以在日期类中,实现<<和>>重载打印日期和提取日

int main()
{
  Date d1(2024, 3, 10);
    //void operator<<(ostream& out);
    //cout<<d1;
    
  d1 << cout;//->d1.operator<<(cout)->operator<<(const Date *d1,cout);
  return 0;
}
  • 如果使用运算符重载,隐含的this指针占用第一个参数的位置,Date必须是左操作数,d1<是不符合我们的习惯的
  • 对此我们可以在类外实现该运算符重载函数,就可以自己设计参数部分的位置
  • 但是又引出了另一个问题:类外不能访问类中的私有成员,如果将私有权限放开,就缺乏安全性,对此C++中有友元,接下来我们会涉及到,这里就使用下,虽然这个全局函数不在类中,但是可以访问私有成员函数
//友元,告诉该类这两个全局函数是我们的朋友,允许使用私有成员(在类中)
  friend ostream& operator<<(ostream& out, const Date& d);
  friend istream& operator>>(istream& in, Date& d);
ostream& operator<<(ostream& out, const Date& d)
{
  out << d._year << "" << d._month << "" << d._day << "" << endl;
  return out;
}
 
istream& operator>>(istream& in, Date& d)
{
  in >> d._year >> d._month >> d._day;
  return in;
}

如果我们需要连续打印cout<的话,这里就不合适的。因为这里的结合性是从左往右,cout<会返回一个临时变量,那么这里运算符重载函数需要通过引用返回了。C++存在私有的,printf不支持自定义打印,cout本质实现所用类型的打印。

- **用引用做返回值,应对连续流插入和流提取
- 流提取不是不能对Date进行const修饰,需要通过键盘读取数据存储在成员变量

6.1 需要判断输入进去的数据是否有误

七、源代码展示

7.1 Date.h

#pragma once
#include<iostream>
#include<assert.h>
using namespace std;
class Date
{
public:
  Date(int year = 1, int month = 1, int day = 1);
  bool operator<(const Date& d);
  bool operator<=(const Date& d);
  bool operator>(const Date& d);
  bool operator>=(const Date& d);
  bool operator==(const Date& d);
  bool operator!=(const Date& d);
  // d1 + 100
  Date& operator+=(int day);
  Date operator+(int day);
  // d1 - 100
  Date operator-(int day);
  Date& operator-=(int day);
  // ++d1
  Date& operator++();
  // 特殊处理:解决语法逻辑不自洽,自相矛盾的问题
  // d1++
  // 为了跟前置++区分,强行增加一个int形参,够成重载区分
  Date operator++(int);
  Date operator--(int);
  Date& operator--();
  // d1 - d2
  int operator-(const Date& d);
  // 本质就是inline
  int GetMonthDay(int year, int month)
  {
    assert(month > 0 && month < 13);
    static int monthDays[13] = { 0, 31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31 };
    // 365   自转  公转  365 5+h
    // 366
    if (month == 2 && ((year % 4 == 0 && year % 100 != 0) || (year % 400 == 0)))
    {
      return 29;
    }
    return monthDays[month];
  }
  void Print()
  {
    cout << _year << "/" << _month << "/" << _day << endl;
  }
private:
  int _year;
  int _month;
  int _day;
};


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

相关文章
|
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++】实现日期类相关接口
|
1天前
|
安全 编译器 C++
【C++篇】C++类与对象深度解析(三):类的默认成员函数详解
【C++篇】C++类与对象深度解析(三):类的默认成员函数详解
9 3