Design and Implement a class called Date that has data members to store month (as a number), day, year, and name of the month. The class should have a three-parameter constructor that allows the data to be set at the time of new Data object instances are created. Default constructor that does not take any parameters should set the default values of 1(month),1(day),2001(year). The class should have following three member functions to display date following formats
showDate1() should display the date in 1/1/2001 format
showDate2() should display the date in January 1,2001 format
showDate3() should display the date in 1 January 2001 format
Also the class should have method to set the date(setDate()).This method should take month, day, and year as parameters and set the object instance data member values.
I can not get my code to return the (8, 29, 1986) in the format required by the question.
My Date.h header file:
#include <string>
using namespace std;
class Date {
public:
Date();
Date(int , int, int);
void setMonth(int);
int getMonth();
void setDay(int);
int getDay();
void setYear(int);
int getYear();
My output is:
1/1/2001
February 12, 2010
12 February 2010
The correct output should be:
1/1/2001
February 12, 2010
29 August 1986
I don't know why my d1.setDate(8, 29, 1986) is not being set. Any help would be greatly appreciated. I have watched alternative videos and read the other posts in this forum but I couldn't find one that helped with this problem. Thanks again.
Please use code tags. Can use the <> button on the format toolbar.
You haven't posted all the code: Where is the code for the 3 showDate functions?
A function can only return one value. getDate could take some references as arguments, you could set these, then they will be available in the calling scope. The getDate function wasn't asked for in the assignment.
There is no need to call set functions in any member function including the constructors, these have direct access to the member variables. An even better idea to use a member initialisation list.
The default constructor was supposed to set values 1,1,2001 , but you do it in the class definition. This makes the default constructor redundant.
With your code for the showDate2 and showDate3 functions, you could have a private function which has that switch statement to set Month. Each of the constructors could call this function, then you won't have repetition of your code. If you have repetition, then there is a better way :+)
Now your actual problem is rather simple :+)
1 2
d1.setDate(8, 29, 1986);
d2.showDate3(); // <----- 12 February 2010 need d1 instead
I thought I had to use the getDate line of code to retrieve the setDate stored information.
Why? You don't call the function, IMO it's not needed at all.
For the getDate line, I was trying to follow the teachers notes but when I removed it, the code compiled anyway with the same results. I am still confused on what the actual problem is.
Wait, am I supposed to change the d2 to d1?! That part of the code was provided by the instructor. I guess I assumed that the provided code was correct.
You were right. I changed the d2.showDate3(); to d1.showDate3(); and the code compiled as it should. I spent a 3 days thinking my code was wrong and it looks to be a typo on the provided code. I can sleep easy now.
I gather you can see that d2 is Feb 12 2010 ? The code works fine (except I would have the private function like I mentioned) Important to realise here that just because code works, it doesn't mean it is right :+)
For the getDate line, I was trying to follow the teachers notes but when I removed it, the code compiled anyway with the same results. I am still confused on what the actual problem is.
The problem is that one can only return 1 value form a function.
What actually happens when the compiler encounters return month, day, year; is the comma operator comes into effect. With the comma operator only the right hand expression is considered. So in this case year is returned.
The comma operator is used in other situations where the side effects of the expressions (other than the last one) are relied upon. Side effects usually happen when there is assignment in one of the expressions. So in your case nothing happens, apart form year being returned.
Your code compiles, but it wouldn't have the right effect, had you actually called the function. Because you never called the function, it doesn't make any difference to anything. Hence why I think it unnecessary.
With the code that sets the Month name: an easier thing to do would be to have an array of Month name strings, then look up the name via the array subscript. This is better than having a big long switch.