Why can I Set the attribute "int X" in "Class2"?
"Class2" doesn't have the variable member "int X"
"Class2" is derived class from Class1. And "int X" was defined on "Class1", and as private, not as protected.
because Class2 inherits Class1, so therefore it inherits all the properties of Class1
so it can be seen like this:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15
class Class1 {
public:
int GetX() { return x; }
void SetX(int a) { x = a; }
private:
int x;
};
class Class2 : public Class1 {
public:
int GetX() { return x; }
void SetX(int a) { x = a; }
private:
int x;
};
and since you inherit the Class1 as public, you can access the members the same as Class1
I don't understand the difference between "private" and "protected"
private members can only access within the function of the class and friends of class
protected member can only access within the function of the class and derived classes