472,145 Members | 2,026 Online
Bytes | Software Development & Data Engineering Community
Post +

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 472,145 software developers and data experts.

Always making members private/protected?

Hi!

My prof told me always to make my members private or protected cause
its standard to write setter and getter methodes.. Is that in your
opinion correct? Cause I dont see any adventages to write a member of a
class private if there are no side effects in changing the variable. I
think you only have to write more (the getter and setters) the code
gets bigger...

Thanks very much!

May 25 '06 #1
12 1981
to**********@vol.at wrote:
My prof told me always to make my members private or protected cause
its standard to write setter and getter methodes..
That would be a wrong reason. But the advice is sound.
Is that in your
opinion correct?
Partially.
Cause I dont see any adventages to write a member of
a class private if there are no side effects in changing the
variable. I think you only have to write more (the getter and
setters) the code gets bigger...


I think what your prof doesn't tell you is that what members to have
is secondary to what objects of your class are supposed to look like
(and *act* like) to other objects in your program. Design not from
the class itself, but from the way it's interacting with the rest of
your model.

Once the public stuff is finalized (through design), you begin the
implemenation phase and there you decide what *else* your class will
need to provide (mostly to itself through private members, or to its
descendants through protected ones).

V
--
Please remove capital 'A's when replying by e-mail
I do not respond to top-posted replies, please don't ask
May 25 '06 #2
to**********@vol.at wrote:
Hi!

My prof told me always to make my members private or protected cause
its standard to write setter and getter methodes..
"Never" and "Always" are seldom good advices.
Is that in your
opinion correct? Cause I dont see any adventages to write a member of a
class private if there are no side effects in changing the variable. I
think you only have to write more (the getter and setters) the code
gets bigger...


I think the best answer would be: it depends. The advantage of using
member functions is that you can control the access to member objects
or even completely hide them. The more you hide implementation details,
the easiest it is to change them. However, the more abstraction layers
you add, the more complex the code is. You have to make a compromise
between abstraction (genericity) and simplicity (usability).
Jonathan

May 25 '06 #3
On 25 May 2006 11:12:15 -0700, "to**********@vol.at"
<to**********@students.fh-vorarlberg.ac.at> wrote:
Hi!

My prof told me always to make my members private or protected cause
its standard to write setter and getter methodes.. Is that in your
opinion correct? Cause I dont see any adventages to write a member of a
class private if there are no side effects in changing the variable. I
think you only have to write more (the getter and setters) the code
gets bigger...

Thanks very much!


Then you need to learn about OOP, probably outside of the classroom
environment which won't prep you for any suitable real-world tasks.

Quick analogy: Would it be beneficial for the passengers of an
aircraft to interface with the cockpit instruments? What would happen
if they /did/ press the big red button marked "jettison fuel tanks"?
May 25 '06 #4
to**********@vol.at posted:
Hi!

My prof told me always to make my members private or protected cause
its standard to write setter and getter methodes.. Is that in your
opinion correct?

I gather that he suggest you write:

class Monkey {
private:

int a;
char b;

public:

void SetA( int const arg ) { a = arg; }

int GetA() const { return a; }

void SetB( char const arg ) { b = arg; }

char GetB() const { return b; }
};
rather than simply:
class Monkey {
public:

int a;
char b;

};
This is ridiculous and amounts to overkill most of the time. But there
are places where it's prefereable to use Getters&Setters. For instance,
maybe in the future, you will rewrite your class such that you no longer
store your values in simple member variables... you might store and
retrieve them from a database for instance. If you have Getters&Setters,
then your client can be totally oblivious to the change in
implementation:

class Monkey {
public:

char GetB() const
{
global_database.open();

global_database.verify_record(17);

return global_datebase.current_record.value();
}

};
However this is extremely rare -- far too rare to adopt a strategy of
ALWAYS writing Getters & Setters. If anything, it demonstrates the
programmers lack of intelligence -- he can't think for himself because
he's too busy adhering to doctrine.

Cause I dont see any adventages to write a member of a
class private if there are no side effects in changing the variable.

And you're correct.

I think you only have to write more (the getter and setters) the code
gets bigger...

And you're correct.
I myself have never written code with Getters&Setters. I even prefer to
have "read-only" members rather than Getters on their own. For instance:

class Monkey {
string str;

public:

const string& cstr;

Monkey() : cstr(str) {}
};

Now "str" is modifiable within the class, but constant to the outside
world.
-Tomás
May 25 '06 #5
Tomás posted:

class Monkey {
public:

char GetB() const
{
global_database.open();

global_database.verify_record(17);

return global_datebase.current_record.value();
}

};

Actually I still wouldn't use Getters&Setters here -- I'd prefer an inner
class which can implicitly convert to "char":

class Monkey {

class Ape {

Ape() {}

Ape(char const arg) : b(arg) {}

operator char() const
{
global_database.open();

global_database.verify_record(17);

return global_datebase.current_record.value();
}
};
public:

Ape b;

};
Or something along those lines. Functions are for algorithms -- not for
finding another way of setting a variable's value.

I never have used Getters&Setters, and it doesn't look likely that I ever
will.
-Tomás
May 25 '06 #6
to**********@vol.at wrote:
My prof told me always to make my members private or protected cause
its standard to write setter and getter methodes..


No, the better way in general is to provide methods that does some action or
obtain some information, and the user of the class must not care if they
read or modify directly a member variable or not.

By the way, protected variable members are also not recommended. Read for
example the comments in "The C++ programming language" about that.

--
Salu2

Inviato da X-Privat.Org - Registrazione gratuita http://www.x-privat.org/join.php
May 25 '06 #7
Tomás wrote:
to**********@vol.at posted:

Hi!

My prof told me always to make my members private or protected cause
its standard to write setter and getter methodes.. Is that in your
opinion correct?


I gather that he suggest you write:

class Monkey {
private:

int a;
char b;

public:

void SetA( int const arg ) { a = arg; }

int GetA() const { return a; }

void SetB( char const arg ) { b = arg; }

char GetB() const { return b; }
};
rather than simply:
class Monkey {
public:

int a;
char b;

};


Probably would want to use a struct here. More obvious.
May 25 '06 #8
Joe Van Dyk posted:

class Monkey {
public:

int a;
char b;

};


Probably would want to use a struct here. More obvious.

I first wrote it using "struct", but right before I clicked "Send", I
realised that there was a chance the original poster would come back with
"What's the difference between class and struct?". For this sole example, I
found it preferable to use "class". (You can't teach someone everything at
once -- just one bit at a time).
-Tomás

May 25 '06 #9

"to**********@vol.at" <to**********@students.fh-vorarlberg.ac.at> wrote in
message news:11**********************@i39g2000cwa.googlegr oups.com...
Hi!

My prof told me always to make my members private or protected cause
its standard to write setter and getter methodes.. Is that in your
opinion correct? Cause I dont see any adventages to write a member of a
class private if there are no side effects in changing the variable. I
think you only have to write more (the getter and setters) the code
gets bigger...

Thanks very much!


First of all, let me say that a good old fashioned C struct is OK by me. I
use them myself for working with legacy code and writing C-compatible
interfaces. They aren't objects but they are perfectly valid data
structures. However, for the rest of this message let's assume you are
interested in using C++ for object oriented programming.

The real objective of object oriented programming is maintainability. It is
hoped that by separating the interface from the implementation one can
modify the interface without breaking any client code. This is the reason
(not "side effects") which makes public data items a bad, or at least
non-object-oriented, idea. By the way, in spite of you professor's comment,
protected data members are no better and for the same reasons.

Your professor is probably right that it is "standard" to access each data
member with a get/set pair, but it is an abhorrent convention. Such
interfaces focus attention on the implementation. The function of the
interface is to HIDE the implementation! Get/set pairs completely destroy
the illusion that the object is anything more than what Stroustrop calls a
"bucket of bits". You seem mystified by why they are so much better than
public data. I am with you.

You may think that object oriented dogma aside, as a practical matter it is
often necessary to use get/set pairs. Believe it or not, I write a lot of
C++ objects for my work and I literally NEVER use get/set pairs. Of course I
would if I found a practical reason why they were required, but so far it
hasn't come up. For instance:

class complex
{
public:
complex(double re, double im);
double re() const;
double im() const;
private:
...
};

Although I would never write my own complex type with such a good one
available in the standard library, I assert that this is a perfectly usable
object interface with no performance issues and, as you can see, no get/set
pairs.

Cy
May 26 '06 #10
I like to use smth like that:

class A
{
int var;
public:
int Var();
void Var(int);
}

May 26 '06 #11
to**********@vol.at wrote:
My prof told me always to make my members private or protected cause
its standard to write setter and getter methodes.. Is that in your
opinion correct? Cause I dont see any adventages to write a member of a
class private if there are no side effects in changing the variable. I
think you only have to write more (the getter and setters) the code
gets bigger...


I think others have answered the basic question here adequately.
"Always" is a bit strong, and your prof is wrong about getters and
setters for the design reasons mentioned already by others in this
thread, but most of the time your data members should be private
(usually not protected, for the same reason they're usually not
public).

Besides saying "me too," I want to take the opportunity to mention a
piece of phrasing I came across which I feel concisely embodies one of
the most valid exceptions to this rule of thumb. I believe I came
across it in Koenig's _Ruminations on C++_. In a discussion on this
guideline, the author mentions an exception for (simple) classes for
which the structure *is* the interface. This is basically just a way
of describing the "classic C-type struct," but I find the notion of
structure as interface to be a useful and novel take on an old idea.

But yeah, apart from exceptions like that, hide 'em away. If you find
yourself wanting to write (many) getters and setters, interpret that as
pain which is a hint to reexamine and improve your design.

Luke

May 26 '06 #12
On 2006-05-25 20:12, to**********@vol.at wrote:
Hi!

My prof told me always to make my members private or protected cause
its standard to write setter and getter methodes.. Is that in your
opinion correct? Cause I dont see any adventages to write a member of a
class private if there are no side effects in changing the variable. I
think you only have to write more (the getter and setters) the code
gets bigger...


If you have a getter/setter-pair for nearly all attributes of your class
and few other methods then you should probably consider making it a pure
data-container (and perhaps indicate this by using struct) with all
attributes public. If you have many attributes compared to the number of
getters/setters you should probably keep them.

Erik Wikström
--
"I have always wished for my computer to be as easy to use as my
telephone; my wish has come true because I can no longer figure
out how to use my telephone" -- Bjarne Stroustrup
May 26 '06 #13

This discussion thread is closed

Replies have been disabled for this discussion.

Similar topics

14 posts views Thread by David Gausebeck | last post: by
10 posts views Thread by Abelardo Vacca | last post: by
8 posts views Thread by Jordan | last post: by
reply views Thread by Saiars | last post: by

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.