There could be several reasons why your MacBook Pro is restarting when charging, including software or hardware issues. Here are some steps you can take to troubleshoot the issue:
1. Check for software updates: Make sure your MacBook Pro is up to date with the latest software updates from Apple. This can often resolve any issues that are caused by outdated software.
2. Reset the System Management Controller (SMC): The SMC controls various hardware functions, including power management. Resetting the SMC can sometimes resolve issues with power management.
3. Check the charging cable and adapter: Make sure the charging cable and adapter are working properly. Try using a different charging cable or adapter to see if the issue persists.
4. Check for overheating: Overheating can cause your MacBook Pro to shut down unexpectedly. Make sure your MacBook Pro is not overheating by checking the temperature in the Activity Monitor app. If your MacBook Pro is overheating, try using it in a cooler environment or using a cooling pad.
If none of these steps resolve the issue, it's possible that there may be a hardware issue with your MacBook Pro. In that case, I recommend contacting Apple support or visiting an Apple Store for further assistance.