Implementing get/set function.

Pages: 12
How is the best way to implement get/set functions?

class Birthday
{
public:
voidPrintBirthdate() const;
bool getBirthYear()
{
return year;
}
bool getBirthMonth()
{
return month;
}
bool getBirthDay()
{
return day;
}
private:
void setBirthYear();
void setBirthMonth();
void setBirthDay();

int year;
int month;
int day;
};

Or would it be something like

bool getBirthYear()
{
if (year < 1900) return 0;

return 1;
}






If you need properties then write your program in C#.:)

As for your code it is obvious that it is invalid. For example what is the sense of the following function?

1
2
3
4
bool getBirthYear()
 {
 return year;
 } 


It will return always true if year is not equal to 0.
Last edited on
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.
What is the great sense to convert year of type int to bool?!!!
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;

else return 0;
}
???
Last edited on
You should probably choose a better name. getBirthYear() would be expected to return the birth year.

If you are testing, checking, validating, verifying something, then use a name whuich has that sort of meaning.
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.
But instead of year you return a bool value.
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.
Right, if it is wrong (before 1900) then it is false and is invalid. If after 1900 it is true and returns the value. No?
What value does it return?!!!
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.
Last edited on
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.
Last edited on
Getters return some data from the class, as it is.

Setters set some data in the class, to whatever was inputted. Here you can get creative, by modifying the data, and returning a false for bad data.
Here is the whole text.

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.
So I am chasing my tail? Any suggestions on how to respond to this and make this into a workable scenerio?
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".

Example:
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
#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)
            return false;
        else
            setBirthYear(tempYear);

        return true;
    }

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!!
Just for completeness... what I consider to be "the right way" (for the code in my previous post).

setBirthYear() expects to be fed a year; meaning the user input happens somewhere outside the class.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
#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;
};


Anyway, don't forget about these:
http://cplusplus.com/doc/
http://en.cppreference.com/w/
http://www.parashift.com/c++-faq/
http://stackoverflow.com/
Pages: 12