I know this example is correct , but I am still not understanding exactly why the first one is not allowed. Why doesn't C++ compiler let you modify the data with "=" here? Please give me an explanation as long as possible.
That's the whole point of having private members. They're only accessible to other members. If there's no significative invariant to keep track, there's really no need to make data private. Typically, you'd use setter functions to make sure no invalid data can be passed (like a negative number for salary, for instance).