Which keyword makes class members accessible to only its derived class members? | Sololearn: Learn to code for FREE!
New course! Every coder should learn Generative AI!
Try a free lesson
+ 2

Which keyword makes class members accessible to only its derived class members?

19th Jan 2018, 2:05 PM
Vrushabh Desai
Vrushabh Desai - avatar
11 Answers
+ 1
class Parent { protected: vec2 m_Position; int m_Health; public: int GetHealth(); void SetHealth(int hp) { m_Health = hp }; vec2 GetPosition(); void SetPosition(vec2 pos) { m_position = pos}; }; class Child : public Parent { private: int m_Armor; public: int GetArmor(); }; This is a crude example how protected variables work. The child class would be able to use the health and position of the parent class and be able to set it for its own use. DONT forget to use ' : public ClassName ' for being able to inherit from the parent class. Parent would have its own position set and child would have its own position set, same goes for health. Hope this helps!
20th Jan 2018, 6:52 PM
Al Z
Al Z - avatar
+ 4
I think it is protected.
19th Jan 2018, 2:41 PM
Jacob Pembleton
Jacob Pembleton - avatar
+ 3
use derived class mode as private
20th Jan 2018, 8:40 AM
Rohit Jarariya
Rohit Jarariya - avatar
+ 2
it is "protected". Anything in a parent class that is protected (variables or methods) will be accessible to any other classes inheriting from the parent (children classes).
20th Jan 2018, 6:44 PM
Al Z
Al Z - avatar
+ 2
Thankyou everyone@Al Z
21st Jan 2018, 12:20 AM
Vrushabh Desai
Vrushabh Desai - avatar
+ 1
yeah right. But how to use it? any example?
19th Jan 2018, 2:50 PM
Vrushabh Desai
Vrushabh Desai - avatar
+ 1
I believe its Friend by using parentClass::childClass
19th Jan 2018, 11:06 PM
Harmony Moyo
Harmony Moyo - avatar
+ 1
protected type
20th Jan 2018, 5:25 PM
navaneetha krishna
navaneetha krishna - avatar
+ 1
and navaneetha and rohit
21st Jan 2018, 12:21 AM
Vrushabh Desai
Vrushabh Desai - avatar
0
#include<iostream> using namespace std; int main () {count << "Hello World"; return 0; }
19th Jan 2018, 7:54 PM
Reza Heshmati
Reza Heshmati - avatar
0
سلام
19th Jan 2018, 7:55 PM
Reza Heshmati
Reza Heshmati - avatar