class Man
{
int age;
public:
void SetAge(int val);
int GetAge() const;
};
class Man
{
int age;
public:
void Age(int val);
int Age() const;
};
class Man
{
int age;
public:
void SetAge(int val);
int GetAge() const;
};
class Man
{
int age;
public:
void Age(int val);
int Age() const;
};
Quote:Original post by DevFred
Why does the getter have an int parameter?
man.age = 100;std::cout << man.age << std::endl;
Quote:Original post by The Communist Duck
If you don't have to do something to the variable when you get/set, what's wrong with:man.age = 100;std::cout << man.age << std::endl;
?
Just seems a little OTT for me, unless there's a reason to make it private.
Then again, I'm not exactly great at this stuff. :P
void Man::Age(int val){if(val < 0) throw std::out_of_bounds("Age can't be negative.");age = val;}
class Person { int age;public: void age(int n); int age() const;};
void doSomethingCool(Person& person){ person.age(42);}
Quote:
And finally, for const members make the type inside Accessors const rather than the Accessors object. I'll leave it to you to work out reasons for this.
Quote:Original post by Windryder
This is ambiguous -- did the class designer intend for age(N) to mean "set age to N" or did s/he mean "advance age by N"? The problem is that age is both a verb and a noun. By contrast, a get/set-prefixed function's purpose is unambiguous.