Default Constructors and Class

closed account (LyboE3v7)
My programs ask for the user input, if it does not meet the criteria it sets its default to 1/1/1500. However, when I run it and enter the criteria it gives me the default parameter.

#ifndef DATE_H
#define DATE_H

class Date
{
private:
int day;
int month;
int year;

public:
Date();
Date(int, int, int);

void isLeap(int);
void displayMonth(int, int, int);

void setMonth(int);
void setDay(int);
void setYear(int);

int getMonth();
int getDate();
int getYear();


};

#endif /* DATE_H */



#include "DATE.h"
#include <iostream>
#include <cstdlib>
using namespace std;
Date::Date()
{
month = 1;
day = 1;
year = 1500;
}
Date::Date(int Month, int Day, int Year)
{
month = Month;
day = Day;
year = Year;

}
void Date::isLeap(int year)
{
if (year % 4 == 0)
{
if (year % 100 == 0)
{
if (year % 400 == 0)
cout << year << " is a leap year." << endl;
else
cout << year << " is not a leap year." << endl;
}
else
cout << year << " is a leap year." << endl;
}
else
cout << year << " is not a leap year." << endl;
}

void Date::displayMonth(int month, int day, int year)
{
if (month == 1)
{
cout << " January " << day << "," << year << endl;
}
else if (month == 2)
{
cout << " February " << day << "," << year << endl;

}
else if (month == 3)
{
cout << " March " << day << "," << year << endl;

}
else if (month == 4)
{
cout << " April " << day << "," << year << endl;

}
else if (month == 5)
{
cout << " May " << day << "," << year << endl;
}
else if (month == 6)
{
cout << " June " << day << "," << year << endl;
}
else if (month == 7)
{
cout << " July " << day << "," << year << endl;
}
else if (month == 8)
{
cout << " August " << day << "," << year << endl;
}
else if (month == 9)
{
cout << " September " << day << "," << year << endl;
}
else if (month == 10)
{
cout << " October " << day << "," << year << endl;
}
else if (month == 11)
{
cout << " November " << day << "," << year << endl;
}
else if (month == 12)
{
cout << " December " << day << "," << year << endl;
}
else
cout << " Giving you default Month ." << endl;

}

void Date::setMonth(int m)
{
if ( m >= 1 && m <= 12)
month = m;
else
{
cout << "Invalid month" << endl;
exit(EXIT_FAILURE);
}

}

void Date::setDay(int d)
{
if (d >= 1 && d <= 30)
day = d;
else
{
cout << "Invalid day" << endl;
exit(EXIT_FAILURE);
}

}
void Date::setYear(int y)
{
if ( y >=1500 && y <= 3000)
year = y;
else
{
cout << "Invalid year" << endl;
exit(EXIT_FAILURE);
}

}

int Date::getMonth()
{
return month;
}
int Date::getDate()
{
return day;
}
int Date::getYear()
{
return year;
}



#include <cstdlib>
#include <iostream>
#include "DATE.h"
using namespace std;

int main()
{
int userMonth;
int userDay;
int userYear;

cout << "Enter a month" << endl;
cin >> userMonth;
cout << "Enter a day" << endl;
cin >> userDay;
cout << "Enter a year" << endl;
cin >> userYear;

Date objInfo;
objInfo.isLeap(objInfo.getYear());
objInfo.displayMonth(objInfo.getMonth(),objInfo.getDate(),
objInfo.getYear());

cout << objInfo.getMonth() << "-" << objInfo.getDate()
<< "-" << objInfo.getYear() << endl;
return 0;
}
Your setters are the one checking for the range, but you didn't use your setters at all. You need to set the variables the user input to for objInfo to check if it's in range.
Please type your code inside the tags [code]
closed account (LyboE3v7)



I hope this is what you mean.
Last edited on
closed account (LyboE3v7)
Nevermind I figured it out.
Topic archived. No new replies allowed.