So would this be better
Bool getBirthyear()
{
if (year < 1900)return 0;
else return year;
}
Sorry, have had little experience with using bool as a return type. Has not come up much in class yet, but want to get it before it does.
It is being used more as a range check. If someone is born before 1900 then it does one thing, is after 1900 it does another. Or for bool getBithDay, comes back invalid if input is less that one or more than 31.
Maybe it would be better as:
bool setBirthYear(int)
{
if (year < 1900) return 1;
I do not understand Chervil, I am wanting to get a birth year. It just has to be afer 1900, that is what I am supposed to be checking, along with a valid day and month.
No, I don't understand either. If you want to get a year, you need some type of variable which can hold 4 digits. Bool can hold just one bit, a yes/no, true/false value. You can't squeeze 4 digits into single bit.
It is supposed to validate the year being after 1900. This is where I am getting confused. Bool is supposed to be true or false. The Birthday class is supposed to accept only dates after 1900. Also supposed to make sure the day and month are valid as well. This goes back to the "encapsulation, along with the incorpration of range checking logic, will help create smarter objects". I am supposed to use bool to return a true or false to check the data that is being input and make sure it is valid and then return it. The 3 functions in the public members are supposed to retrieve specific data from the user and check to see if it is acceptable (i.e. year is after 1900, month is not less than 1 or greater than 12). Return false if the information given in invalid. The functions in the private member are called by by their corresponding get functions after the input is verified. Believe me, I am way more confused that you are at the moment.
Well then, you should validate the year in the constructor for the class. And you might also validate the year in the setBirthYear() function.
Note, neither of these will return anything at all.
Do you have the text of the question, or a description of the project you are working on? It may be that you've misunderstood what you are supposed to do.
Class Birthday
{
public:
// Display birthday
voidPrintBirthdate() const;
/* 3 functions to retrieve specific data from the user and check to see if it is acceptable
(i.e. year is not greater than the current year, month is not less than 1 or greater than
12). Return false if the information given in invalid*/
bool getBirthYear();
bool getBirthMonth();
bool getBirthDay();
Private:
// Set functions are called by their corresponding get functions after the input is verified
void setBirthYear();
void setBirthMonth();
void setBirthDay();
// Declare variables
int year;
int month;
int day;
};
The three member variables (year month and day), as well as the set functions, are encapsulated and protected from the user and cannot directly access or modify them. Only from accessing the get function and having the data verified can the user then have any of these variables changed. The range checks have been correctly described on what they do, now show the implementation.
Ha ha!
So the getBirthX() functions are actually setters, and from the looks of it they are supposed to get input from the keyboard, and they call the private setBirthX() functions ... which are missing formal parameters... to set the private data members ("variables").
FUBAR, my friend. This is not the way to do it, and especially not the way to teach it.
Two minor issues: 1) respect the case. e.g. class not Class, private not Private. 2) in C++, bool is true or false, not 1 or 0.
Right... your assignment is stupid, as in "not how you should do it".
You get user input, and "validate" it, in your getBirthX() functions.
Then you pass whatever you get to your setBirthX() functions, which finally set the "variables".
#include <iostream>
// using namespace std; // uncomment this to remove std:: 's
class BirthDay {
public:
bool getBirthYear() // you can define your functions inside the class body
{
int tempYear = 0;
std::cout << "Input the year: ";
std::cin >> tempYear;
if (tempYear < 1900 || tempYear > 2025)
returnfalse;
else
setBirthYear(tempYear);
returntrue;
}
private:
void setBirthYear(int y)
{
year = y;
}
int year;
};
Thanks Catfish, now I am worried I am being taught the wrong way to do things. Unless that is what he is trying to make us see. As for the cases, is me typing into Word and not paying attention to auto correct, the bool result is a brain fart on my end. Guess I am getting too frustrated. Appreciate your patience and help!!
#include <iostream>
class BirthDay {
public:
// getter of year
int getBirthYear() const
{
return year;
}
// setter of year
void setBirthYear(int y)
{
// validate 'y' and change its value if needed
year = y;
}
private:
int year;
};