The doc told me to wait 4weeks before having sex after d&c. Why so?

Infection. The cervix (the opening to the uterus) is slightly open after a dilation and curettage. Intercourse will increase the risk of an infection after a d&c. Even if you have stopped bleeding and cramping, there is still a risk.
Sex after D&C. Common reasons include that it might be uncomfortable, it might lead to bleeding, it might lead to infection in the uterus. If everything feels well and without any abnormal discharge, you are very unlikely to experience any significant complication from an earlier attempt at sex. If it goes well, repeat as necessary.