473,382 Members | 1,648 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,382 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 2095
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 thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

14
by: David Gausebeck | last post by:
The inability to denote data members as read-only in C++ is something which has annoyed me (slightly) for a while now. You can always get around it with accessor methods that return const...
5
by: Suzanne Vogel | last post by:
Hi, Given: I have a class with protected or private data members, some of them without accessor methods. It's someone else's class, so I can't change it. (eg, I can't add accessor methods to the...
11
by: Roger Leigh | last post by:
The C++ book I have to hand (Liberty and Horvath, Teach yourself C++ for Linux in 21 Days--I know there are better) states that "static member functions cannot access any non-static member...
19
by: qazmlp | last post by:
class base { // other members public: virtual ~base() { } virtual void virtualMethod1()=0 ; virtual void virtualMethod2()=0 ; virtual void virtualMethod3()=0 ;
10
by: Abelardo Vacca | last post by:
Hi, The title sums up the question pretty much. I would like to access all private members of a class including the private members of its base classes.( I already have the ReflectionPermission )...
6
by: Ken Varn | last post by:
I have an ASP.NET application that is calling a custom class that is trying to parse all of the members of my Page object using Type.GetMembers(). The problem that I am having is that private...
11
by: Kevin Prichard | last post by:
Hi all, I've recently been following the object-oriented techiques discussed here and have been testing them for use in a web application. There is problem that I'd like to discuss with you...
8
by: Jordan | last post by:
AFAIK there are two ways to expose members of a base class to a derived or child class: 1. declare the members public in the base class 2. declare them as 'protected' in the base class Is...
12
by: Premal | last post by:
Hi, I tried to make delete operator private for my class. Strangely it is giving me error if I compile that code in VC++.NET. But it compiles successfully on VC++6.o. Can anybody give me inputs...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 3 Apr 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome former...
0
by: ryjfgjl | last post by:
In our work, we often need to import Excel data into databases (such as MySQL, SQL Server, Oracle) for data analysis and processing. Usually, we use database tools like Navicat or the Excel import...
0
by: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...

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.