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

Recommended Answers

All 2 Replies

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)

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.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.