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
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
^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
gg ^^
12/12/2006 4:11:58 PM
so basically just east of use with more chance of someone stealing your code?
12/12/2006 4:12:01 PM
Another questions, I am making an inheritance chart and started and goes as follows..... student undergrad graduate freshmen sophomore junior senior Masters Doctoralhow else can this be extended, might be a stupid question.....
12/12/2006 4:22:19 PM
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
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
private means that only methods within the base class can change itprotected means that only methods within the base class and/or any derived classes can change itpublic means anything can change iti think
12/12/2006 4:57:17 PM
^ 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
thanksneeded clarification for extra credit project
12/14/2006 2:23:27 PM
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 scopeclass derived2 : public base {}; // base class protected members are public in derived class scope
12/14/2006 5:57:28 PM