First, understand that it is completely normal and developmentally appropriate for a two-year-old to hit. Your son isn't old enough to really understand "sharing" and, even though he can talk, he likely doesn't yet have the developmental capability to put his feelings into words.
Of course, this doesn't mean that you let him hit other kids. By remaining close by and engaged in his play, you will often be able to intervene before your son lashes out at another child. In the event that he does hit another child, you can model empathy and issue an apology to set the example for him. You can help your son put his feelings into words and continue to work with him on sharing (or "taking turns," which is sometimes an easier concept to understand). By staying calm and comforting his distress, you help regulate his emotions and model empathetic behavior.
Hitting your son would teach him to fear you rather than trust you and would model that violence is an acceptable way to change someone else's behavior. Hitting will anger and humiliate him, but it won't be effective at teaching him to regulate his emotions or to control his impulses.
It can be very frustrating and even embarrassing when your child hits other children, but by modeling appropriate behavior yourself, other parents will understand and appreciate how you go about it. With your calm help and loving guidance combined with time for his natural cognitive development, your son will eventually learn to share. For now, prevention and compassion are your best tools.