Help with Destructors

Hi all. I am working on an assignment for my C++ class. I have the code written but am unsure why the destructor is not outputting when an item has been destroyed. I would appreciate it if somebody would be able to review this code for me and give me some feedback.

First off here is our assignment description:
________________________________________________________________________________
Write a class called DATE that contains month, day and year as private integer variables.

It should have 3 constructors.
Constructor #1 is a null constructor (no parameters) that sets the date to: 1/1/1970.
Constructor #2 has only a month and year as arguments and sets the date to: Argument1/1/Argument2
Constructor #3 gets month, date and year as arguments

Write public functions to set and get the date variables (setMonth, setDay, setYear, getMonth, getDay, getYear)
Write a public function that will print the instance of DATE (printDate).

Create a destructor.
Create a copy constructor.
Create an assignment overload.
Create an == comparison overload.
Create any other “operator” overload.

Create a main program that will use instances of the class and show that all of the public functions you have designed work.
________________________________________________________________________________

Here is my code:
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
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
#include <iostream>
using namespace std;

//*********************************************************************************
//								Class DATE Creation
//*********************************************************************************

class DATE
{

	private:

	   int Month, Day, Year;


	public:
		
//*********************************************************************************
//								Constructor Definitions
//*********************************************************************************
	 	DATE();
	 	DATE(int, int);
	 	DATE(int, int, int);


//*********************************************************************************
//								Copy Constructor Definition
//*********************************************************************************
	 	DATE(const DATE &obj)
	 	{
	  		cout<< "Copy Constructor"<< endl;
	 		
	 	    Month= obj.Month;
	 		Day= obj.Day;
	 		Year= obj.Year;
	 	}

//*********************************************************************************
//								Destructor Definition
//*********************************************************************************

	 	~DATE();

//*********************************************************************************
//					Operator Overloads (Comparison, Assignment & Extra Credit
//*********************************************************************************
	 	bool operator==(DATE &coDate1);				//Comparison

		DATE& operator = (const DATE &aoDate);		//Assignment

		bool operator>(DATE &coDate2);				//Extra Credit
	 	
 	
//*********************************************************************************
//								Public Functions
//*********************************************************************************

		//	Set Date
		int setDay();
		int setMonth ();
		int setYear ();

		//	Get Date
		int getDay()
		{return Day;}						
						
		int getMonth()
		{return Month;}
	
		int getYear()
		{return Year;}
		
		// Print Date
		void printDate();
		
		
};

//*********************************************************************************
//					Setting Date to 01/01/1970
//*********************************************************************************
DATE::DATE()
{
	    
	   Month = 01;
	   Day = 01;
	   Year = 1970;
}

//*********************************************************************************
//					Constructor with Month & Year
//*********************************************************************************

DATE::DATE(int month, int year)
{
	   Month = month;
	   Day = 1;
	   Year = year;
}

//*********************************************************************************
//					Constructor with Month, Day & Year
//*********************************************************************************

DATE::DATE (int month, int day, int year)
{
	   Month = month;
	   Day = day;
	   Year = year;
}

//*********************************************************************************
//					Overloads (Comparison, Assignment & Extra Credit
//*********************************************************************************

DATE& DATE::operator =(const DATE &CSource)
{
	Month = CSource.Month;
	Day = CSource.Day;
	Year = CSource.Year;
	
	cout << "Overloaded Assignment"<< endl;

	return *this;
}


bool DATE::operator== (DATE &coDate1)
{
	cout << "Overloaded Comparison" << endl;
	
	return (Month == coDate1.Month);
}

bool DATE::operator> (DATE &coDate2)
{
	cout << "Overloaded Extra Credit" << endl;
	
	return (Day > coDate2.Day);
}

//*********************************************************************************
//							Destructor
//*********************************************************************************

DATE::~DATE()
{
	   cout << "This has been destructed" << endl;
} 


//*********************************************************************************
//							Function Definitions
//*********************************************************************************

int DATE::setMonth()
{
	int x_month;

	cout<< "Enter the month: " ;
	cin >> x_month;
	Month = x_month;
	
	return Month;
}

int DATE::setDay()
{
	int x_day;

	cout<< "Enter the day: " ;
	cin >> x_day;
	Day = x_day;
	
	return Day;
}

int DATE::setYear()
{
	int x_year;

	cout<< "Enter the year: " ;
	cin >> x_year;
	Year = x_year;
	
	return Year;
}

void DATE::printDate()
{
	cout << "Your Date is: "<< getMonth() << "/" << getDay() <<
	"/" << getYear() << endl;

	return;
}

//*********************************************************************************
//								Main Function
//*********************************************************************************

int main()
{
	DATE	finalDate;		//DATE class definition
	
	
	finalDate.printDate();	//Print the Default Date

//*********************************************************************************
//							Set Date with Functions
//*********************************************************************************

	// Setting Date
	finalDate.setMonth();	
	finalDate.setDay();
	finalDate.setYear();
	
	// Printing Date
	finalDate.printDate();
	
	// Copy Constructor
	DATE finalDatetwo(finalDate);
	
	// Printing Date
	finalDatetwo.printDate();
	
	// Constructor with Two arguments
	DATE	datetwoargs(4,2010);
	
	// Constructor with Three arguments
	datetwoargs.printDate();
	
	DATE	datethreeargs (8,13,1980);
	
	// Printing Date
	datethreeargs.printDate();
	
	// Overloads
	DATE dateoverload;
	
	 //Assignment Overload
	dateoverload = finalDate;
	
	// Comparison Overloads
	dateoverload.printDate();
		
	if (finalDate== datetwoargs)
		cout << "The date is equal to 4/1/2010" << endl;
		
		else
	    
	    cout << "The date is not equal to 4/1/2010" << endl;
	    
		    
	if (finalDate > datethreeargs)
		cout << "The date is later than 8/13/1980"<< endl;
		
		else
		
		cout << "The date is earlier than 8/13/1980" << endl;

	system ("pause");
	return 0;
					
}
How do you run the program?


Note: your assignment and comparison ops could use some refinement.
The objects will be destroyed at the return from main, which is after the call to system("pause").

If you want them to be destroyed before system("pause") you can define the objects in a more limited scope by using curly brackets.
1
2
3
4
5
6
7
8
9
10
11
int main()
{
	{
	DATE	finalDate;		//DATE class definition
	
	...	
	}
	system ("pause");
	return 0;
				
}
Last edited on
What do you mean how do I run the program?

What is wrong with the ops?
How would I make it so that I could see the cout for the destructor?

I got everything in the C++ class up until we hit classes. My understanding of C++ went downhill when we hit classes, constructors, destructors, etc...
"How run prog" "How start app" ... there are several ways to do it. One is clickety clap gui crap. The other is to have terminal open all the time and type commands on the prompt. That way you see all the stdout on the terminal.

What happens if your code does a = a;?

op< is the one that should be implemented. Standard library header provides op>. Is 6/14/1900 later than 6/13/2012?
Topic archived. No new replies allowed.