How can I get the default constructor to "set" the values for a date?

Hey folks. I could use a pointer here. Been sitting here for a while trying to figure out which approach to take next.

We've been given a project in which we utilize a class with a default constructor. The objective is to have a three-parameter default constructor which allows a date to be set. If the user creates a Date object without passing any arguments, or if any arguments, or if any of the values passed are invalid (which will be specified by input validation).

Been scratching my head on this one. Here's what I've come up with thus far:

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
#include <cstdlib>
#include <iostream>

using namespace std;

class Date

{
      private:                //Reserved for private variables
            
            int month;       //These variables will hold values for month, day,
            int day;         //and year.       
            int year;      
      
      public:               //Reserved for public funtions 
                 
      Date(int, int, int); //Default constructor
       
      
};             


Date::Date(int month = 1, int day = 2, int year = 2001) //Defining constructor

{     
            
            
      if (month = 1, day = 1, year = 2001) //Assigning arguments to default   
                                           // constructor
       
        {  cout << "Date: \n\n";
           
           cout << month << "/" << day << "/" << year << endl ; 
           
           cout << "January" << " " << day << "," << year << endl ; 
           
           cout << day << " " << "Janurary" << " " << year << endl ; 
        
         }
               
               
               
            
}
 
         
int main()

{     
    Date dateinfo; //Defining object 
    
    
    cout << "\n";
    system ("pause");           
    return 0; 
    
}




What I'm trying to figure out from this point is how I can get the default constructor to set the values.

I thought of creating another function and making a call to that function within the default constructor but that would look ridiculous to have output displayed and then prompt the user for setting the dates.

Anyone have any ideas or clues? This is practically self-study.
Date(int, int, int); //Default constructor

This is not the default constructor. The default constructor is the one which can take no arguments to be called.
Date(int, int, int); //Default constructor Is not a default constructor, it is a three argument constructor, a default constructor takes no parameters.

I don't know why an instructor would give you a task with this kind of constructor. Maybe you need this?

1
2
3
4
5
Date(); // Default constructor
Date(int nYear); // 1 arg, year constructor
Date(int nYear, int nMoth); //2 arg, constructor
Date(int nYear, int nMonth, int nDay); //3 arg, constructor


Either way back to the topic, if what you have is what you were given (meaning the signature for that constructor) then consider renaming the names of the parameters in your constructor.

1
2
3
4
Date::Date(int month = 1, int day = 2, int year = 2001) //These are the same as the actual member variables

//How about this...
Date::Date(int newMonth = 1, int newDay = 2, int newYear = 2001) 


Now as far as assigning, after you check the validity of the data based on whatever criteria you were assigned... using an if like this if( newMonth == 1 and not if(newMonth = 1) that is an assignment not a condition.

Then just do year = newYear and the same for the others.
Okay, I see what you're getting at. By definition that is true. Gotcha.

Perhaps I should've posted the directions for the program. Sorry for leaving out the details.

Problem #9, Homework #3

Design a class called Date that has integer data members to store month, day, and year. The class should have a three-parameter default constructor that allows the date to be set at the time a new Date object is created. If the user creates a Date object without passing any arguments, or if any of the values passed are invalid, the default values of 1, 1, 2001 should be used. The class should have member functions to print the date in the following formats.

That explains the:

Date(int, int, int); //Default constructor


I figured that if they wanted me to use the default constructor, which does not take arguments unless specified otherwise that I would have to place in the parameters and assign default arguments.


Three parameter default constructor:
MyClass(int a = 0, int b = 0, int c = 0){}
It takes three parameters, AND it is the default constructor. All in one. ;)

Here's how:
1
2
3
MyClass instanceA; //calls the default constructor by implicitly passing 0 for all three args
MyClass instanceB (1, 2, 3); //calls the three-arg constructor,
                             //which is the default one in this case 
Last edited on
Thanks guys, going to take these things into consideration and give it another go here shortly.
Hey guys, thanks again for your uhelp. I've managed to get the program to compile but am now having an issue displaying the required input for the program.

The program requires that we specify the date in these formats:

3/15/10
March 15, 2010
15 March 2010

I've managed to get the first line of output. But I'm having issues providing the next two lines of output. Here's my output:



1/1/2012
Press any key to continue . . .



Here's my source 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

#include <iostream>
#include <iomanip>

using namespace std; 


class Date

{
      private: 
               int day, month, year; //Variables to be held
               
               
      public: 
              Date(int = 0, int = 0, int = 0); //Prototype for default constructor 
              void setDay(int); 
              void setMonth(int); 
              void setYear(int); 
              int getDay();  
              int getMonth(); 
              int getYear(); 
              void ShowDateInfo();
              void ShowDateInfoFormat2();
              
};              

Date::Date(int nDay, int nMonth, int nYear) //Default constructor will make call to functions. This is where function implementation begins.

{

 cout << "\n\n";
 setDay(nDay);
 setMonth(nMonth);
 setYear(nYear); 
 int getDay();             
 int getMonth();             
 int getYear();
 void ShowDateInfo();
 ShowDateInfoFormat2();
 
}        


void Date::setDay(int nDay)               //This function will allow set value for day. First to be called.

{ 
     if (nDay >= 1 && nDay <= 30)
        day = nDay;
     else
           day = 1; 
}           
     
void Date::setMonth(int nMonth)          //This function will allow the month to be set. Second to be called. 

{
     if (nMonth >= 1 && nMonth <=12)
        month = nMonth; 
     else
           month = 1; 
}

void Date::setYear(int nYear)           //This function will allow the year to be set. Third to be called
{
     if (nYear > 1950 && nYear < 2020)
        year = nYear; 
     else
          year = 2001;
}          
          
int Date::getDay()                           //This function will allow value for day to be retrieved
{
    return day;
            
}              
       
int Date::getMonth()

{         
 
    return month; 
 
}


int Date::getYear()

{
    
    return year; 
    
}            
          
void Date::ShowDateInfo()

{    
     cout << getDay() << "/" ;
     cout << getMonth() << "/" ;
     cout << getYear() << "\n" ; 
     
}              
          
void Date::ShowDateInfoFormat2()
          
{          
           
   
}          
          
          
int main()

{
    //Date dateObj;
    Date dateObj2(1, 20, 2012);
    dateObj2.ShowDateInfo();
    dateObj2.ShowDateInfoFormat2();

          
    system("pause");
    
    return 0;
        
                         
}     
       


What I'm trying to do now is figure out how I can display the other two lines of output required for the program.

What I've thought of thus far is using a if else-if control structure and placing it in a completely different function (similiar to the ShowDateInfo() function) but my output is really funky. Displaying something like this:



Janurary1/1/2012
JanuraryPress any key to continue . . .




It seems like creating a completely different function and having the object call to that function it fudges up the output completely when executed. I know this is extremely trivial and can be easily solved but can someone give me a pointer?





Simple mistake. Figured it out. Again, thanks for your help!
Topic archived. No new replies allowed.