954,480 Members — Technology Publication meets Social Media
Username:
Password:
Lost login information?
Have something to say? Contribute New Article Reply to this Article

Clearification with information hiding

PLEASE HELP!!!

Ok, I've been racking my brain for the past two or three days now on this question, and before you click the back button, I might add I'm not looking for the answer, I'm looking for clearification of the question... here's the question:

"In a paragraph or two, discuss the tradeoffs between implementing the above classes using protected data (which allows derived classes to access them directly) versus creating member functions in the base class (but maintaining data members as private)."

Looking at this, I keep thinking with any class you implement using protected data, and when you create a base class (or any other class for that matter, you also create member functions within them. So I can't really make sense of what it's asking... Then it says below the question...

"Hint: Perhaps you gain efficiency by one method, but lose information hiding."

????
Any help is greatly appreciated - as it helps to stop the pain,
Thanks,
Nate

Coach_Nate
Light Poster
48 posts since Aug 2004
Reputation Points: 14
Solved Threads: 1
 
Looking at this, I keep thinking with any class you implement using protected data, and when you create a base class (or any other class for that matter, you also create member functions within them. So I can't really make sense of what it's asking... Then it says below the question... Basically u use protected members when you want that immidiately derived class should access the private data members in some way "Hint: Perhaps you gain efficiency by one method, but lose information hiding."


-Private members of the base class are not accessible in the derived class (to preserve encapsulation)
-Sometimes, however, we would like to be able to define encapsulated data members which are not publicly accessible, but which are accessible to derived classes:

class A
{
public:
       A():x(0),y(0){}
       void IncrVal()
        {
                ++x;
                ++y;
        }     
private:
        int x;
        int y;
};
class B : public A
{
public:
        B():z(0){}
       void change() 
        {
                ++x;                 // ACCESS ERROR!
        }
private:
        int z;
};

-So C++ provides a third access specifier: protected:

- "Protected" members are not accessible from outside the class, except in derived classes.

-So a protected member which is inherited, is accessible in the derived class (and remains protected there)

SpS
Posting Pro
599 posts since Aug 2005
Reputation Points: 70
Solved Threads: 32
 

Protected data is convenient because it's easy to access, but it breaks all kinds of good programming practices in the process.

>Hint: Perhaps you gain efficiency by one method, but lose information hiding.
Translation: Perhaps your instructor has no idea what an inline function is. This has nothing to do with efficiency and everything to do with safety, maintainability and robustness.

Narue
Bad Cop
Administrator
15,460 posts since Sep 2004
Reputation Points: 6,464
Solved Threads: 1,401
 

This article has been dead for over three months

Post: Markdown Syntax: Formatting Help
You