Until a student comes into my office, it doesn't matter what course my office hours are for: I am not preparing for them in any way. Most likely I am trying to do "side work": i.e., short, routine tasks that are easily set aside as soon as a student steps in. If only one student comes in, it doesn't matter what class they're taking. In fact, it probably doesn't matter if they are taking any class from me at all: if a student wants to stop by my office for any quick reason (e.g. to sign paperwork), then I often tell them to come during my office hours because I know I'll be there.
The only issue occurs when multiple students arrive. Whenever this happens one has to decide how to "process" the students: serially, in a group, all at once, alternately, and so forth. This is where designating an office hour as being for a certain class becomes useful: when the student flow is expected to be sufficiently large. (If I really think the flow will be minimal, then I am not averse to provisionally scheduling office hours for multiple classes at the same time. E.g. if one class is a graduate class then students will probably stop by very rarely, and if they see I am busy they have an office in the same building and can easily come back after a short time.) If for instance a student from one class came by right at the beginning of my office hours designated for another class, then I would probably warn them that they could get interrupted at any point, and when the students from the other class came I would give them priority.
From the student perspective, office hours are affected by the distinction between research universities and liberal arts colleges. As a professor at a research university, I do not feel obligated to "generally" be in my office during business hours, and if I am I may not keep my door open. (In my department, there is a bit of a split between faculty who are generally available in their office and those that are not. I have found that being in your office with your door open invites a lot of wasted time.) However at a liberal arts college, many faculty are in their offices whenever they're not teaching or out to lunch, with the expectation that students can and will drop by at any time. I visited an old friend at a research college a few years ago, and the situation there was amusingly extreme: the math department student study area was in the center of a beautiful split-level space, with glass blackboards, study materials....:the works. Lining the periphery of this lovely oval were....the faculty offices. Continuing the glass theme, the interior walls of the faculty office were made of glass. Thus: you can't leave your office without walking by the students (who are few enough in number that you know most of them personally), and a student can see at all times whether you're in your office or not! In this kind of setting, coming in during some other class's office hours would be unnecessary and is probably a little rude.
Students can schedule an appointment to meet outside of office hours. My own attitude to this is that if someone contacts me on Day N, then I will offer them an appointment on Day N+2 at the latest, and on Day N+1 unless I will be away from my office or my whole day is booked solid with specific appointments (which is rare). In many cases I would rather book an appointment even a few hours in the future than be interrupted from what I'm doing in my office at that moment. This is mostly psychological, but no less true for that. I'm not sure why students don't do this more often. In fact, having booked appointments, students fail to show up an annoyingly large percentage of the time. That kind of spaciness wears a lot better once you have a PhD.