473,761 Members | 6,993 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

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 2134
to**********@vo l.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**********@vo l.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**********@v ol.at"
<to**********@s tudents.fh-vorarlberg.ac.a t> 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**********@vo l.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**********@vo l.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**********@vo l.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**********@v ol.at" <to**********@s tudents.fh-vorarlberg.ac.a t> wrote in
message news:11******** **************@ i39g2000cwa.goo glegroups.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

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

14
11376
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 references, but classes with huge lists of accessors often seem awkward/ugly. So I finally sat down and thought of a syntax I'd like to see as a fix. I certainly don't expect to ever see it introduced, but it's interesting to think about. The...
5
3652
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 parent class, and I can't make some "helper" class a friend of the parent class to help in accessing the data.) Problem: I want to derive a class that has a copy constructor that properly copies those data members.
11
4614
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 variables". However, this doesn't seem entirely correct. It also doesn't mention whether static member functions can access protected and private member data and methods (and I couldn't spot this in the FAQ). I have a class row<Row> which derives from...
19
2347
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
25743
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 ) Is there a way to get this information ? Thnaks in advance
6
4789
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 members are not returned. I did some digging and the MSDN documentation states that the caller must have ReflectionPermission in order to get the private members of a class. I am a little unfamiliar with this stipulation. I have checked the docs on...
11
3843
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 experts. I would like to produce Javascript classes that can be "subclassed" with certain behaviors defined at subclass time. There are plenty of ways to do this through prototyping and other techniques, but these behaviors need to be static and...
8
16356
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 either of these methods generally recommended over the other? If yes, why? Thanks!
12
2569
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 about it. I wanted that on my class delete should not work. Object pointer should be deleted using my function only which is taking care of reference count for particular class. Thanx in advance for your inputs.
0
9554
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main usage, and What is the difference between ONU and Router. Let’s take a closer look ! Part I. Meaning of...
0
9376
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
1
9923
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
0
8813
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own.... Now, this would greatly impact the work of software developers. The idea...
1
7358
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 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 a new presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes instead of User Defined Types (UDT). For example, to manage the data in unbound forms. Adolph will...
0
5405
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
3911
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system
3
3509
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
3
2788
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating effective websites that not only look great but also perform exceptionally well. In this comprehensive...

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.