Chariot
Chariot

Reputation: 335

Adding days to a date operator+

I am trying to make a operator+ to add days to a date. I have a "Date" class:

#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;
}

So, I know the operator it's incomplete, but i only want to compile by the moment. I tried to do the operator+ but i don't know really well For example if the date is 2/4/2018 and days is 20, i want to add and the result will be 22/4/2018.

main:


#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;
}

I hope you understand me.

Upvotes: 0

Views: 848

Answers (1)

DDMC
DDMC

Reputation: 393

You need to update the fields manually. Add d to m_day, advance m_month and subtract the number of days in current month from m_day until the date is valid. Note that you need to update m_year when the month exceed 12.

Below is the code I've rewritten. Besides the changes to solve your problem, I moved the function definitions to the cpp file, leaving only declarations in the .h file. As the iostream has nothing to do with Date class (at least until now), I removed its inclusion from the header file.

// 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;
}

Upvotes: 1

Related Questions