【C++初阶学习】C++类和对象实战-Date类的实现(2)

简介: 【C++初阶学习】C++类和对象实战-Date类的实现(2)

7、Date+=天数


  • 注意:


  1. +=表示会修改Date本身的数据


  1. 处理传入负数天数


  1. 处理好天数进位,月份进位


  • 实现代码:


//日期+=天数
Date& Date::operator+=(int day)
{
  if (day < 0)//处理特殊情况
  {
    *this -= -day;//复用Date-=天数
  }
  else
  {
    _day += day;
    while (_day > GetMonthDay(_year, _month))//处理数据合理性
    {
      _day -= GetMonthDay(_year, _month);
      _month++;
      if (_month > 12)
      {
        _year++;
        _month = 1;
      }
    }
  }
  return *this;//返回引用,即对象本身
}


8、Date+天数


  • 注意:


  1. +天数表示不会修改Date本身的数据(使用const修饰,避免修改)


  1. 逻辑与Date+=天数基本一致,可以进行复用


  • 实现代码:


Date Date::operator+(int day) const
{
  Date tmp = *this;//赋值重载
  tmp += day;//复用+=重载
  return tmp;//返回值(拷贝构造)
}


9、Date-=天数


  • 注意:


  1. +=表示会修改Date本身的数据


  1. 处理传入负数天数


  1. 考虑日期的借位,月份的借位


  • 实现代码:


//日期-=天数
Date& Date::operator-=(int day)
{
  if (day < 0)
  {
    *this += -day;//复用Date+=天数
  }
  else
  {
    _day -= day;
    while (_day <= 0)//处理数据合理性
    {
      _month--;
      if (_month <= 0)
      {
        _year--;
        _month = 12;
      }
      _day += GetMonthDay(_year, _month);
    }
  }
  return *this;
}


10、Date-天数


  • 注意:


  1. -天数不会修改Date本身的数据(使用const修饰,避免修改)


  1. 逻辑与Date-=天数基本一致,可以进行复用


  • 实现代码:


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


11、++Date


  • 注意:


前置++表示,Date先增后使用


  • 实现代码:


//++Date
Date& Date::operator++()
{
  *this += 1;//复用Date+=天数
  return *this;
}


12、Date++


  • 注意:


  1. 语法规定,因为与前置命名相同的缘故,这里的后置函数多一个参数来与前置函数形成重载


  1. 后置++表示先使用后自增


  • 实现代码:


//Date++
Date Date::operator++(int)
{
  Date tmp = *this;//保存一份日期
  *this += 1;//自增当前日期
  return tmp;//返回自增前的日期
}


13、–Date


  • 实现代码:


//--Date
Date& Date::operator--()
{
  *this -= 1;
  return *this;
}


14、Date–


  • 实现代码:


//Date--
Date Date::operator--(int)
{
  Date tmp = *this;
  *this -= 1;
  return tmp;
}


15、日期比较


注:可以多次复用


  • 实现代码:


//日期比较
bool Date::operator>(const Date& d) const
{
  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;
}
bool Date::operator==(const Date& d) const
{
  return _year == d._year && _month == d._month && _day == d._day;
}
bool Date::operator<(const Date& d) const
{
  return !(*this >= d);
}
bool Date::operator>=(const Date& d) const
{
  return *this > d || *this == d;
}
bool Date::operator<=(const Date& d) const
{
  return !(*this > d);
}
bool Date::operator!=(const Date& d) const
{
  return !(*this == d);
}


16、Date相减


  • 实现代码:


 //日期减日期
 int Date::operator-(const Date& d) const
 {
  //确定日期的大小
  Date max = *this;
  Date min = d;
  if (*this < d)//复用日期比较
  {
    max = d;
    min = *this;
  }
  int day = 0;
  while (max != min)
  {
    ++min;
    ++day;
  }
  return day;
 }


17、日期输入\日期输出


  • 注意:


对于输入操作符,我们习惯是cin>>date,而这样的用法表示做操作数是cin,右操作数为日期对象,但是对于类成员函数来说,存在着隐含参数this指针(占据和第一个参数位置,即日期对象是左操作数)


虽然定义成类外函数能修改参数位置,但是无法访问类里的私有成员变量,这里我们使用友元函数来解决,即在类里声明函数前加上friend,便可以访问成员


实现代码:


//输出操作符重载
ostream& operator<<(ostream& _cout, const Date& d)
{
  _cout << d._year << "年" << d._month << "月" << d._day << "日" ;
  return _cout;
}
//输出操作符重载
istream& operator>>(istream& _cin, Date& d)
> {
  _cin >> d._year >> d._month >> d._day;
  return _cin;
}


效果图:

date,而这样的用法表示做操作数是cin,右操作数为日期对象,但是对于类成员函数来说,存在着隐含参数this指针(占据和第一个参数位置,即日期对象是左操作数)


虽然定义成类外函数能修改参数位置,但是无法访问类里的私有成员变量,这里我们使用友元函数来解决,即在类里声明函数前加上friend,便可以访问成员


实现代码:


//输出操作符重载
ostream& operator<<(ostream& _cout, const Date& d)
{
  _cout << d._year << "年" << d._month << "月" << d._day << "日" ;
  return _cout;
}
//输出操作符重载
istream& operator>>(istream& _cin, Date& d)
{
  _cin >> d._year >> d._month >> d._day;
  return _cin;
}


效果图


image.png

相关文章
|
2天前
|
测试技术 C++
C++|运算符重载(3)|日期类的计算
C++|运算符重载(3)|日期类的计算
|
4天前
|
C语言 C++ 容器
C++ string类
C++ string类
9 0
|
4天前
|
C++ Linux
|
4天前
|
编译器 C++
【C++】继续学习 string类 吧
首先不得不说的是由于历史原因,string的接口多达130多个,简直冗杂… 所以学习过程中,我们只需要选取常用的,好用的来进行使用即可(有种垃圾堆里翻美食的感觉)
9 1
|
4天前
|
算法 安全 程序员
【C++】STL学习之旅——初识STL,认识string类
现在我正式开始学习STL,这让我期待好久了,一想到不用手撕链表,手搓堆栈,心里非常爽
16 0
|
4天前
|
存储 安全 测试技术
【C++】string学习 — 手搓string类项目
C++ 的 string 类是 C++ 标准库中提供的一个用于处理字符串的类。它在 C++ 的历史中扮演了重要的角色,为字符串处理提供了更加方便、高效的方法。
18 0
【C++】string学习 — 手搓string类项目
|
4天前
|
Java C++ Python
【C++从练气到飞升】06---重识类和对象(二)
【C++从练气到飞升】06---重识类和对象(二)
|
4天前
|
编译器 C++
【C++从练气到飞升】06---重识类和对象(一)
【C++从练气到飞升】06---重识类和对象(一)