This is a quick reply, sorry I’m not posting code.
You can scan the image pixel by pixel, and generate an output image with the desired result: copy the pixel from foreground unless it is transparent, in which case copy from background.
You can also produce a mask from foreground alpha channel, and use the mask to blend the images, all with Mat arithmetics.
Remember, OpenCV is not an image processing library, but a computer vision one. It’s more about extracting meaning from images.