Private members are only accessible within the class defining them.
Protected members are accessible in the class that defines them and in classes that inherit from that class.
Edit: Both are also accessible by friends of their class, and in the case of protected members, by friends of their derived classes.
Edit 2: Use whatever makes sense in the context of your problem. You should try to make members private whenever you can to reduce coupling and protect the implementation of the base class, but if that’s not possible then use protected members. Check C++ FAQ for a better understanding of the issue. This question about protected variables might also help.
Related Posts:
- Understanding Python super() with __init__() methods [duplicate]
- When should you use a class vs a struct in C++?
- C++ [Error] no matching function for call to
- Separating class code into a header and cpp file
- Error “C++ requires a type specifier for all declarations whilst defining methods”
- What are the differences between struct and class in C++?
- How do I define string constants in C++?
- C++ variable has initializer but incomplete type?
- C++ – No appropriate default constructor available
- What is predicate in C++?
- How do you implement a class in C?
- C++ classes (public, private, and protected)
- Error: invalid use of member in static member function
- What does the variable $this mean in PHP?
- What is the purpose of the word ‘self’?
- Is it possible to make abstract classes in Python?
- Expression must have class type
- C++ error ‘Undefined reference to Class::Function()’ [duplicate]
- Using two CSS classes on one element
- c++ “Incomplete type not allowed” error accessing class reference information (Circular dependency with forward declaration)
- Why is enum class preferred over plain enum?
- How to assign multiple classes to an HTML container?
- What does ‘super().__init__()’ mean in python 3.x?
- How to assign multiple classes to an HTML container?
- Reading from file in c++ ifstream
- Why am I getting this redefinition of class error?
- Are static class variables possible in Python?
- Single class has a Class Redefinition Error
- C++ Linked List Node with class
- (->) arrow operator and (.) dot operator , class pointer
- Is C++ an Object Oriented language?
- Does Python have “private” variables in classes?
- Python: How do I make a subclass from a superclass?
- how to define -std=c++11 as default in g++
- Pointer to incomplete class type is not allowed
- Why am I getting this redefinition of class error?
- Difference between ‘cls’ and ‘self’ in Python classes?
- no default constructor exists for class
- Error at constructor : Expected an identifier?
- Getting error “a nonstatic member reference must be relative to a specific object” while both member are in the same class
- C++ BlackJack Stuck trying to program Ace
- error C2011: ” : ‘class’ type redefinition
- When should I use ‘self’ over ‘$this’?
- How to implement a binary search tree in Python?
- The compiler is complaining about my default parameters?
- Already defined in .obj – no double inclusions
- C++ Linked List Node with class
- Getting error: ISO C++ forbids declaration of with no type
- fatal error LNK1169: one or more multiply defined symbols found in game programming
- How to call a parent class function from derived class function?
- “used without template parameters”
- Python function overloading
- expected constructor, destructor, or type conversion before ‘(’ token
- C++ Class ‘undeclared identifier”
- c++ “Incomplete type not allowed” error accessing class reference information (Circular dependency with forward declaration)
- Base class undefined
- Warning: non-static data member initializers only available with -std=c++11 or -std=gnu++11?
- What is the meaning of a C++ Wrapper Class?
- Does C++11 have C#-style properties?
- G++ undefined reference to class::function
- Creating an instance of class
- Why do some classes require main methods and others do not?
- Pass arguments to Constructor in VBA
- uml classdiagram constructor with parameters
- expected primary-expression before ‘]’ token
- When/why to make function private in class?
- Does C have classes?
- error C2039: ‘string’ : is not a member of ‘std’, header file problem
- istream and ostream problem – C++
- Struct with template variables in C++
- error: no member function declared in class
- Here is some error with my .h file which show [Error] unterminated #ifndef when I include my class template in it
- How can I design a class named allergy?
- C++ equivalent of java’s instanceof
- Is “delete this” allowed in C++?
- What is a segmentation fault?
- How many spaces for tab character(\t)?
- How to create a dynamic array of integers
- How to create a dynamic array of integers
- Linker Error C++ “undefined reference ” [duplicate]
- How do I build a graphical user interface in C++? [closed]
- C++ convert from 1 char to string?
- How do I build a graphical user interface in C++? [closed]
- convert a char* to std::string
- system(“pause”); – Why is it wrong?
- Pause Console in C++ program
- system(“pause”); – Why is it wrong?
- Pause Console in C++ program
- How to implement 2D vector array?
- Why the switch statement cannot be applied on strings?
- What is the difference between g++ and gcc?
- How to use setprecision in C++
- How to dynamically allocate arrays in C++
- What does (~0L) mean?
- How to dynamically allocate arrays in C++
- What is the best way to use a HashMap in C++?
- What is the best way to use a HashMap in C++?
- What are the differences between a pointer variable and a reference variable in C++?
- What does ‘super’ do in Python? – difference between super().__init__() and explicit superclass __init__()
- Why do we need virtual functions in C++?