类和对象(5):Date类详解

简介: 类和对象(5):Date类详解
// Date.h
#include <iostream>
using namespace std;
#include <assert.h>
class Date
{
public:
  Date(int year = 1, int month = 1, int day = 1)
  {
    assert(month >= 1 && month <= 12);
    _year = year;
    _month = month;
    _day = day;
  }
  void Print()
    {
        cout << _year << " " << _month << " " << _day << endl;
    }
  int GetMonthDay(int year, int month);
  bool operator==(const Date& d);
  bool operator>(const Date& d);
  Date& operator+=(int x);
  Date operator+(int x);
  Date& operator-=(int x);
  Date operator-(int x);
  Date& operator++();
  Date operator++(int);
    int operator-(const Date& d);
private:
  int _year;
  int _month;
  int _day;
};

1. operator==、operator>

bool Date::operator==(const Date& d)
{
  return _year == d._year
    && _month == d._month
    && _day == d._day;
}
bool Date::operator>(const Date& d)
{
  if (_year > d._year)
  {
    return true;
  }
  else if (_year == d._year && _month > d._month)
  {
    return true;
  }
  else if (_year == d._year && _month == d._month && _day > d._day)
  {
    return true;
  }
  return false;
}

!=>=< 等运算符,皆可都通过对==>进行逻辑运算复用实现。

2. operator+= (/+)、operator-= (/-)

int Date::GetMonthDay(int year, int month)
{
  assert(month >= 1 && month <= 12);
  int Day[13] = { 0, 31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31 };
    // 判断闰二月
  if (month == 2 && (year % 400 == 0 || (year % 4 == 0 && year % 100 != 0)))
  {
    return 29;
  }
  return Day[month];
}
Date& Date::operator+=(int x)
{
  if (x < 0)
  {
    return *this -= (-x);
  }
  _day += x;
  while (_day > GetMonthDay(_year, _month))
  {
    _day -= GetMonthDay(_year, _month);
    _month++;
    if (_month == 13)
    {
      _year++;
      _month = 1;
    }
  }
  return *this;
}
Date Date::operator+(int x)
{
  Date tmp(*this);
  tmp += x;
  return tmp;
}
Date& Date::operator-=(int x)
{
  if (x < 0)
  {
    return *this += (-x);
  }
  _day -= x;
  while (_day <= 0)
  {
    _month--;
    if (_month == 0)
    {
      _month = 12;
      _year--;
    }
    _day += GetMonthDay(_year, _month);
  }
  return *this;
}
Date Date::operator-(int x)
{
  Date tmp(*this);
  tmp -= x;
  return tmp;
}

此处有一种重点,以**+=—>+** 和 **+—>+=**进行对比举例。(暂不考虑 x < 0 的情况)

Date Date::operator+(int x)
{
    Date tmp(*this);
    tmp._day += x;
  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;
}
Date& Date::operator+=(int x)
{
    *this = *this + x;
    return *this;
}

+=—>+

// += 0次对象拷贝
// + 2次对象拷贝

+—>+=

// + 2次对象拷贝
// += 3次对象拷贝

使用前者的复用方法,效率更高。

3. operator++()、operator++(int)

Date& Date::operator++()// 后置++
{
  *this += 1;
  return *this;
}
Date Date::operator++(int)// 前置++
{
  Date tmp(*this);
  *this += 1;
  return tmp;
}

4. int operator-(const Date& d)

int Date::operator-(const Date& d)
{
  int flag = 1;// 假设左大右小
  Date max(*this);
  Date min(d);
  // 左小右大进行调整
  if (max < min)
  {
    max = d;
    min = *this;
    flag = -1;
  }
  int n = 0;
  while (min != max)
  {
    min++;
    n++;
  }
  return n * flag;
}
相关文章
|
1月前
|
C++ 编译器
【C++语言】Date类的代码实现(操作符重载运用)
【C++语言】Date类的代码实现(操作符重载运用)
|
2月前
|
编译器 C++
【C++初阶】4. Date类的实现
【C++初阶】4. Date类的实现
42 1
|
2月前
|
编译器 C++
【C++类和对象】日期类的实现(上)
【C++类和对象】日期类的实现
|
2月前
|
存储 C++
【C++类和对象】日期类的实现(下)
【C++类和对象】日期类的实现
|
10月前
|
编译器 C++
C++类和对象 练习小项目---日期类的实现.
C++类和对象 练习小项目---日期类的实现.
70 0
|
12月前
|
存储 编译器 C语言
类和对象---初步认识类
类和对象---初步认识类
类和对象---初步认识类
|
11月前
|
编译器 C++
[C++] 类与对象(中)完整讲述运算符重载示例 -- 日期类(Date) -- const成员1
[C++] 类与对象(中)完整讲述运算符重载示例 -- 日期类(Date) -- const成员1
|
11月前
|
安全 编译器 C++
[C++] 类与对象(中)完整讲述运算符重载示例 -- 日期类(Date) -- const成员2
[C++] 类与对象(中)完整讲述运算符重载示例 -- 日期类(Date) -- const成员2
|
11月前
Date类
Date类
46 0
|
编译器 C++
【C++】类和对象练习——日期类的实现(二)
【C++】类和对象练习——日期类的实现(二)
56 0