can anyone explain whether this is correct or else help me to write a proper answer plsss


answer.................


Private data members cannot be accessed outside the class. When a class inherits a base class, all the data members except the private get inherited into it. So if we want data members to be accessible to only derived classes and not privately or publicly accessible, then we can use protected.

Dani AI

Generated

A short, practical distinction and when to pick one over the other.

Private members are for data or helpers that only the defining class (and its friends) should touch. Protected members are intended for things a class author wants subclasses to be able to use or override, while still hiding them from general public use. As observed, private members do exist in derived objects (they are inherited), but derived-class code cannot access them directly.

When to use protected:

  • Use protected for clear extension points: helper functions or state that subclasses must see to implement correct behavior (for example, a protected doX_impl() that derived classes override).
  • Prefer protected virtual functions over protected data. Exposing raw data as protected couples subclasses to the base class implementation and makes future refactoring risky.
  • If only limited access is needed, keep data private and provide protected or public accessor/mutator functions that preserve invariants. That gives control and documents expected subclass behavior.
  • For library/public APIs, avoid protected data members; reserve protected members for well-documented extension points.

Example (illustrates accessibility):

class Base {
private:
    int secret;     // not accessible in Derived
protected:
    int state;      // accessible in Derived
public:
    Base(): secret(42), state(0) {}
};

class Derived : public Base {
public:
    void tweak() {
        // secret = 1;    // error
        state = 10;       // OK
    }
};

In short: protected = controlled exposure for subclasses; private = strict encapsulation. ’s textbook phrasing points in the right direction, and ’s clarification about inheritance vs access is the key practical nuance.

Recommended Answers

All 5 Replies

Hi Precila. Your question makes absolutely no sense at all.:) When you say protected and private, what do you mean by that?

this is one of the questions i got 4 my assignment

so thougth of answering this by explaining what is protected and what is private mean

dont know whether my idea is correct cauz i hav no clue about this

>Private data members cannot be accessed outside the class.
Yes.

>When a class inherits a base class, all the data members except the private get inherited into it.
Private members are inherited, but not accessible from the child class.

>So if we want data members to be accessible to only derived classes
>and not privately or publicly accessible, then we can use protected.

While not incorrect, this answer is the kind of cookie cutter thing you'd get right out of a textbook. It shows knowledge, but not necessarily understanding.

A better answer would give a practical example for using protected, explain why it's preferred, then derive a more general guideline from the explanation. This covers all bases in that it shows you can think of a good situation where protected makes sense (understanding of the concept), defend the decision (understanding the pros and cons versus alternatives), and draw a logical conclusion (show creativity and independent thought).

tanx AndreRet and Narue you guy's were really helpfull tanx a lot 4 helping me to complete this

tanx a lot agian friends

It was a pleasure.:)

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.