User not logged in - login - register
Home Calendar Books School Tool Photo Gallery Message Boards Users Statistics Advertise Site Info
go to bottom | |
 Message Boards » » C++ question Page [1]  
bhswain
All American
1101 Posts
user info
edit post

What are the benefits of using protected access versus private access in base classes?

Is it just the ease of access with protected while still having a moderate level of code security?

[Edited on December 12, 2006 at 3:45 PM. Reason : .]

12/12/2006 3:44:25 PM

philihp
All American
8349 Posts
user info
edit post

don't use protected if you're a parent and you don't want your children touching your privates. only your friends can touch your privates.

12/12/2006 4:00:41 PM

ktcosent2112
All American
628 Posts
user info
edit post

^haha, nice way to put it.

Basically protected means subclasses can access, private means they cannot. Been a while since I've used C++ though

12/12/2006 4:06:07 PM

qntmfred
retired
40559 Posts
user info
edit post

gg ^^

12/12/2006 4:11:58 PM

bhswain
All American
1101 Posts
user info
edit post

so basically just east of use with more chance of someone stealing your code?

12/12/2006 4:12:01 PM

bhswain
All American
1101 Posts
user info
edit post

Another questions, I am making an inheritance chart and started and goes as follows.....

student
undergrad graduate
freshmen sophomore junior senior Masters Doctoral

how else can this be extended, might be a stupid question.....

12/12/2006 4:22:19 PM

philihp
All American
8349 Posts
user info
edit post

public/private member protection doesn't saveguard your code from people reading it... leave that up to someone else at a later stage.

you want to make things private and protected in order to limit the number of ways a program using your classes can use it. ideally you want to be able to account for every 'state' the object can be in, so you can guarantee and prove that it will always work. it's all about encapsulation.

take for example you have a class that holds a letter that is initially 'Y'. you have a method called setLetterToN and a method called setLetterToY, that do what you think they do. you have another method called getLetter, which you want guarantee will return 'Y' or 'N'. if you have your letter variable declared as private, you can guarantee that the only ways to set it are through setLetterToY and setLetterToN, so you can guarantee that it will always be in {YN}. if it were public, someone could come along in another method somewhere else and set it outside of your class.

12/12/2006 4:32:47 PM

bhswain
All American
1101 Posts
user info
edit post

thanks! That answers the question much better then wha I was thinking......I thought it was about code security for some reason.

12/12/2006 4:37:06 PM

BigMan157
no u
103352 Posts
user info
edit post

private means that only methods within the base class can change it

protected means that only methods within the base class and/or any derived classes can change it

public means anything can change it

i think

12/12/2006 4:57:17 PM

synchrony7
All American
4462 Posts
user info
edit post

^ Right, unless you declare another class as a friend of the base class. Then it can access everything in the base class, even if it's private.

You don't use this much, the only time I've done it is to write a tester class that I want to be able to check the status of private variables after a test has been run.

12/14/2006 2:00:29 PM

bhswain
All American
1101 Posts
user info
edit post

thanks
needed clarification for extra credit project

12/14/2006 2:23:27 PM

Bakunin
Suspended
8558 Posts
user info
edit post

if I recall correctly it also depends on how you import the base class:


class base {
protected:
// stuff
};
class derived : private base {}; // base class protected members are private in derived class scope
class derived2 : public base {}; // base class protected members are public in derived class scope


[Edited on December 14, 2006 at 5:58 PM. Reason : too lazy to google it though]

12/14/2006 5:57:28 PM

 Message Boards » Tech Talk » C++ question Page [1]  
go to top | |
Admin Options : move topic | lock topic

© 2024 by The Wolf Web - All Rights Reserved.
The material located at this site is not endorsed, sponsored or provided by or on behalf of North Carolina State University.
Powered by CrazyWeb v2.38 - our disclaimer.