Reputation: 1
I'm trying to do an exercise using classes to store dates (MM DD YYYY). I have two class constructors, one default that sets the Date values to a default, and one that uses explicit values sent from main()
in my driver file.
However, after my declaration statement in main()
to the explicit value constructor (to make date2
) in my driver file, I'm getting errors that appear to indicate that date2
was never made.
Below are the relevant portions for this constructor. How do I get date2
to become defined and declared to use for other things in main()
?
Header
class Date
{
private:
int month, day, year;
public:
Date(); // default date constructor will assign all data members to equivalent of 01/01/2022
Date(int initMonth, int initDay, int initYear); //parametered Date constructor will collectively set all three data members
}
Implementation
Date :: Date(): month(1), day(1), year(2022) {}
Date :: Date(int initMonth, int initDay, int initYear)
{
if (initMonth < 1 || initMonth > 12) { cerr << "Invalid month\n\n"; month = -2; }
if (initDay < 1 || initDay > 30) { cerr << "Invalid day\n\n"; day = -2; }
if (initYear < 1) { cerr << "Invalid year\n\n"; year = -2; }
month = initMonth;
day = initDay;
year = initYear;
}
Driver
Date date1;
do
{
cout << "Please enter a date in this sequence: MM DD YYYY ";
cin >> month >> day >> year;
Date date2(month, day, year);
} while (date2.month == -2 || date2.day == -2 || date2.year == -2);
date1.print();
date2.print();
Upvotes: 0
Views: 140