I'm studying C++ form Thinking in C++ V1. I came across an example that demonstrates inheritance. Here it goes
#include <iostream>
class Instrument{
public:
virtual void play(){
std::cout<<"instrument::play()";
}
};
class Wind: public Instrument{
public:
void play(){
std::cout<<"Wind::play()";
}
};
void tune(Instrument& i){
i.play();
}
int _tmain(int argc, _TCHAR* argv[])
{
Wind flute;
tune(flute);
return 0;
}
This outputs Wind::play()
on the console.
But if I change the method 'tune' to
void tune(Instrument i){
i.play();
}
The output will instrument::play()
Since the '&' is added so that the reference of flute is passed instead of a copy, why does the program output instrument::play()
instead of Wind::play()
?