Hello,
The code example you give, I suspect, is NOT trying to do anything like what you want.
I suspect that it's using a variety of C++. The program seems to be receiving a midi note number, and then converting the note number into a frequency, and then using an oscillator (not a midi instrument or suchlike) to create a sound. I don't see anything that might make the 'sound' be anything different for different instruments.
Your program needs more to receive a basic midi instruction, and then send that same instruction out again to your midi instrument. No oscillators involved. You then need additional code regarding instrument selection, and use of any relevant controllers.
Depending on the midi device involved, you might be able to use simple Program Change to select different instruments, or you might need to use Bank Select instructions as well.
I'm not sure if you need the midi input, and the keyboard controller, to achieve your purpose. I have written versions of software similar to this, for testing/auditioning sounds, and I have used the computer keyboard for input and converted the key pressed into midi note number via a table. Shift and key has generated a chord based on the selected note. Other key combinations has applied certain controllers, or changed volume/pan etc. You can make it all as fancy as you need.
On my various computers I may have different versions, at least one is specific to the Roland LAPC-I soundcard that I have, and the 1,000+ instrument patches that can be loaded. This is using just the midi ports to send out the midi data from the PC to the Roland card. Relatively simple changes could make the prog work with other devices.
Am I descibing something like what you were trying to achieve?
Geoff