0

So i saw this code fragment in Data Structures and Algorithm in c and c++:

class DLinkedList { // doubly linked list
public:
    DLinkedList(); // constructor
    ~DLinkedList(); // destructor
    bool empty() const; // is list empty?
    const Elem& front() const; // get front element
    const Elem& back() const; // get back element
    void addFront(const Elem& e); // add to front of list
    void addBack(const Elem& e); // add to back of list
    void removeFront(); // remove from front
    void removeBack(); // remove from back
private: // local type definitions
    DNode* header; // list sentinels
    DNode* trailer;
protected: // local utilities
    void add(DNode* v, const Elem& e); // insert new node before v
    void remove(DNode* v); // remove node v
};

and my question is: why are the member functions add() and remove() protected (and when I should use the keyword protected)

edit: DNode is defined here:

typedef string Elem; // list element type
class DNode { // doubly linked list node
private:
    Elem elem; // node element value
    DNode* prev; // previous node in list
    DNode* next; // next node in list
    friend class DLinkedList; // allow DLinkedList access
};

4
  • 2
    Maybe the author would like to use inheritance in later chapters. Protected members can be used by derived children. According to en.cppreference.com/w/cpp/language/access: "Protected inheritance: the public and protected members of the base class listed after the access specifier are protected members of the derived class while the private members of the base class are inaccessible to the derived class" Commented Sep 16, 2021 at 14:30
  • 1
    They're helper functions (not intended to be part of the API; you can tell because they deal in Node pointers), but why they're protected and not private is a mystery that only the author can help you with. Commented Sep 16, 2021 at 14:30
  • We like comments, don't we? Commented Sep 16, 2021 at 14:48
  • The decision as to what belongs to the public API and what is protected can be a subjective design decision. You are asking us to guess at the author's design, and there is not a lot of information to go on. My best guess is that the answer lies in the definitions of DNode (used by these protected members) and of Elem (used by some public members). Is it possible to give some more context without straying into an overwhelming code dump? Commented Sep 16, 2021 at 17:12

3 Answers 3

1

you use protected when you don't want the API to view certain methods but do want to access the method from the class and its subclasses(outside or within package) and classes from the same package

the reason add() and remove() are protected is to provide data abstraction and to prevent unauthorized personnel from using these functions

Sign up to request clarification or add additional context in comments.

3 Comments

I get what you mean and this may be a bad question because I'm essentially asking you to guess what the author was thinking when he wrote this book but why shouldn't add() and remove() be accessible to the public?
like you said i guessed it, but i based my guess on how normally softwares work, not all end users have access to all functionality.
For example, a firm uses a priority queue to manage tasks. the employees don't have the privilege to add (enqueue) tasks only certain members do, but they have the privilege to mark tasks as done or dequeue them - this is a pretty loose example irl there are many more complexities
0

SO protected means that it can be used ONLY by derived classes (classes that inherit from class Dlinkedlist in this case) or friends of this class (see declaring friends of class). However, it's hard to tell what the author meant by declaring them protected though

Comments

0

Protected only makes sense if this class is used as a base class for inheritence. The protected methods are accessible only from methods in the class or in derived classes.

I expect you will find that the authors go on to define other example data structures that derive from DLinkedList.

1 Comment

Side note: If future inheritance is intended, you should consider making the destructor virtual to allow for runtime polymorphism.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.