+ 2
Which keyword makes class members accessible to only its derived class members?
11 odpowiedzi
+ 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!
+ 4
I think it is protected.
+ 3
use derived class mode as private
+ 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).
+ 2
Thankyou everyone@Al Z
+ 1
yeah right. But how to use it? any example?
+ 1
I believe its Friend
by using parentClass::childClass
+ 1
protected type
+ 1
and navaneetha and rohit
0
#include<iostream>
using namespace std;
int main ()
{count << "Hello World";
return 0;
}
0
سلام