Operator Overloading Problem. Variable is not changed

I using Operator Overloading + to add a Date object with int num. The Date will be modified. But when I test it, it's not change at all


File DateAndTime.h
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
// Date class declaration

#include "stdafx.h"

#ifndef DATE_H
#define DATE_H

class Date
{
private:
	int day,month,year;
public:
	Date() {};
	Date(int, int, int); //Constructor
	int leapYear();
	int valid();
	void input();
	void output();
	void nextDay(Date &);
	void previousDay(Date &);

	Date operator=(const Date &a);
	Date &operator+(int);
};

#endif 


File DateAndTime.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
// Date class definition

#include "stdafx.h"

#include "iostream"
using std::cout;
using std::cin;
using std::endl;

#include "Date.h"

Date::Date(int d, int m, int y)
	: day(d),
	month(m),
	year(y)
{}

int Date::leapYear()
{
	if(year%400==0 || (year%100!=0 && year%4 == 0))
		return 1;
	else
		return 0;
}

int Date::valid()
{
	if(day<0)
	{
		return 0;
	}
	else
	{
		switch(month)
		{
		case 2:
			{
				if(this->leapYear()==0)
					if(day<29)
						return 1;
					else
						return 0;
				else
					if(day<30)
						return 1;
					else
						return 0;
				break;
			}
		case 1:
		case 3:
		case 5:
		case 7:
		case 8:
		case 10:
		case 12:
			{
				if(day<32)
					return 1;
				else
					return 0;
				break;
			}
		default:
			{
				if(day<31)
					return 1;
				else
					return 0;
				break;
			}
		}
	}
}
			
void Date::input()
{
	do
	{
		cout<<"  Enter Day: ";
		cin>>day;
		cout<<"Enter Month: ";
		cin>>month;
		cout<<" Enter Year: ";
		cin>>year;
		cout<<endl;
		if(this->valid()==0)
			cout<<"Re-enter date to replace invalid date"<<endl;
	}
	while(this->valid()==0);
}

void Date::output()
{
	cout<<"You have entered: "
		<<day<<"/"
		<<month<<"/"
		<<year<<endl;
}

void Date::nextDay(Date &next)
{
	next.day=this->day;
	next.month=this->month;
	next.year=this->year;
	switch(day)
	{
	case 28:
		{
			if(this->leapYear()==0)
				next.day=next.day+1;
			else
			{
				next.day=1;
				next.month=3;
			}
			break;
		}
	case 30:
		{
			if(month==4 || month==6 || month==9 || month==11)
			{
				next.day=1;
				next.month=next.month+1;
			}
			else
				next.month=31;
		break;
		}
	case 31:
		{
			if(month=12)
			{
				next.day=1;
				next.month=1;
				next.year=next.year+1;
			}
			else
			{
				next.day=1;
				next.month=next.month+1;
			}
			break;
		}
	}
}

Date Date::operator=(const Date &a)
{
	Date b;
	b.day=a.day;
	b.month=a.month;
	b.year=a.year;
	return b;
}

Date &Date::operator+(int a)
{
	int sum=0;
	int i;
	int m[]={0,31,28,31,30,31,30,31,31,30,31,30,31};
	m[0]=leapYear();
	if(m[0]!=0)
		m[2]=29;
	for(i=1;i<month;i++)
		sum=sum+m[i];
	sum=sum+day;// The number of day in the year
	sum=sum+a;  // The number of day after plus operator

	// Change back to Date
	i=1;
	while(sum>m[i])
	{
		sum=sum-m[i];
		i++;
	}
	int d=sum;
	int mn=i;
	int y=this->year;
	Date b(d,mn,y);
	return b;
}


Main
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
// Main program
// Version 1.0. Testing

#include "stdafx.h"
#include "iostream"
using std::cout;
using std::cin;
using std::endl;

#include "Date.h"

int main()
{
	Date a;
	a.input();
	a.output();

	Date b=a;
	b.output();

	int num;
	cout<<"Enter number of day you want to add in: ";
	cin>>num;
	Date c;
	c=(b+num); //if I try b=b+num; b still the "old" b (=a)
	c.output();

	return 0;
}


Date c is not change (Output is just the default value of c). Why is that???? Somebody help me.
closed account (D80DSL3A)
The = operator is assigning values to the local variable b. It should make the assignments to the members of this (the object calling the = function).
Also, the return types are reversed. The + operator should return a Date and = should return a Date&. Aren't you getting a warning about + returning a reference to a local variable?
Try:
1
2
3
4
5
6
7
Date& Date::operator=(const Date &a)
{
	day=a.day;
	month=a.month;
	year=a.year;
	return *this;
}


EDIT: Disch recently gave an excellent explanation for the return types of a variety of operators here: http://www.cplusplus.com/forum/beginner/53199/#msg288017
Last edited on
Thank you very much. The link you gave me is an excellent explanation.
Topic archived. No new replies allowed.