The code doesn't cause an error because:
_name is protected in Person.
teacher is a derived class of Person, so it can access protected members of Person.
This is intentional design in C++ to allow derived classes to interact with base class members while still restricting access from outside the hierarchy.