将日期添加到日期运算符+

时间:2018-09-22 20:53:41

标签: c++

我正在尝试让某个操作员+在日期上加上几天。 我有一个“约会”课程:

#include <iostream>

class Date
{
private:
    int m_day;
    int m_month;
    int m_year;


public:
    Date(); 
    Date(int day, int month, int year); 
    Date(const Date& d); 
    void setDay(int day) { m_day = day; };
    void setMonth(int month) { m_month = month; };
    void setYear(int year) { m_year = year; };
    int getDay()const { return m_day; };
    int getMonth()const { return m_month; };
    int getYear()const { return m_year; };

    Date operator+(int d)const;
     };

Date.cpp:

#include "Date.h"

Date::Date() {
    m_day = 0;
    m_month = 0;
    m_year = 0000;
}
Date::Date(int day, int month, int year) {
    m_day = day;
    m_month = month;
    m_year = year;

}
Date::Date(const Date& d) { //constructor de copia
m_day = d.m_day;
m_month = d.m_month;
m_year = d.m_year;
}

Date Date::operator+(int d)const {

    Date result;
    result.m_day = m_day + d.m_day;
    result.m_month = m_month;
    result.m_year = m_year;

    return result;
}

所以,我知道运算符还不完整,但是我现在只想编译。我试图做接线员+,但我不太了解 例如,如果日期是2/4/2018,天是20,我要添加,结果将是22/4/2018。

主要:


#include "Date.h"
#include <iostream>
using namespace std;

int main() {


    Date date1 = { 2 , 3, 1990 };
    int days = 20;
    date1 = date1 + days;
    cout << date1.getDay() << "/" << date1.getMonth() << "/" << date1.getYear()<< "\n";
    system("pause");
    return 0;
}

希望您能理解我。

1 个答案:

答案 0 :(得分:1)

您需要手动更新字段。将d添加到m_day,前进m_month,然后从m_day减去当前月份的天数,直到日期有效。请注意,当月份超过12时,您需要更新m_year

下面是我重写的代码。除了解决您的问题的更改之外,我还将函数定义移至cpp文件,仅在.h文件中保留了声明。由于iostream与Date类无关(至少到目前为止),所以我从头文件中删除了它。

// Date.h
class Date
{
private:
  int m_day;
  int m_month;
  int m_year;

public:
  Date();
  Date(int day, int month, int year);
  Date(const Date& d);
  void setDay(int day);
  void setMonth(int month);
  void setYear(int year);
  int getDay() const;
  int getMonth() const;
  int getYear() const;
  int isInLeapYear() const;
  int getDaysInMonth() const;

  Date operator+(int d) const;
  friend Date operator+(int daysToAdd, const Date& date);
};

// Date.cpp
#include "Date.h"
Date::Date() {
  m_day = 1;
  m_month = 1;
  m_year = 1970;
}
Date::Date(int day, int month, int year) {
  m_day = day;
  m_month = month;
  m_year = year;
}
Date::Date(const Date& d) {
  m_day = d.m_day;
  m_month = d.m_month;
  m_year = d.m_year;
}
void Date::setDay(int day) {
  m_day = day;
}
void Date::setMonth(int month) {
  m_month = month;
}
void Date::setYear(int year) {
  m_year = year;
}
int Date::getDay() const {
  return m_day;
}
int Date::getMonth() const {
  return m_month;
}
int Date::getYear() const {
  return m_year;
}
int Date::isInLeapYear() const {
  return m_year % 400 == 0 || (m_year % 4 == 0 && m_year % 100 != 0);
}
int Date::getDaysInMonth() const {
  switch (m_month) {
    case 1: case 3: case 5: case 7: case 8: case 10: case 12:
      return 31;
    case 4: case 6: case 9: case 11:
      return 30;
    case 2:
      return 28 + isInLeapYear();
  }
  return 0;
}
Date Date::operator+(int d) const {
  Date result = *this;
  result.m_day += d;
  while (result.m_day > result.getDaysInMonth()) {
    result.m_day -= result.getDaysInMonth();
    ++result.m_month;
    if (result.m_month > 12) {
      result.m_month = 1;
      ++result.m_year;
    }
  }
  return result;
}
Date operator+(int daysToAdd, const Date& date) {
  return date + daysToAdd;
}