I always laugh when people complain about the performance of virtual functions and whatnot. I code in Python, where every
function is virtual (even those that aren't inside a class). My code runs fine. Virtual functions in C++ are not going to be a problem unless you're calling them in a tight loop or something, and if you've got a tight loop with a performance problem, you'll know it and be able to do something about it then.
Most people who worry about performance really shouldn't, even if they're writing something like a video game. I wrote a video game entirely in Python and it ran much faster than it needed to, even on my old computer. Granted, it was a 2D game. If you're writing software for cell phones or something, you have reason to worry, because slow code will hog the CPU, which drains the battery, but otherwise...
Now, sometimes inheritance and virtual functions are inappropriate in C++ for other reasons. One is that composition or templates may be a better solution to your specific problem. Also, to use inheritance correctly, you need to know the Liskov Substitution Principle
, which says that if class B derives from class A, then it should be the case that A "is a" B. Sometimes this is counterintuitive. For example, a Rectangle "is a" Shape. But a Square does not satisfy "Square 'is a' Rectangle", even though you'd expect it to, because a square has certain restrictions a rectangle does not. If you change a rectangle's width, it won't change its height -- but a square would have to, meaning a square doesn't always behave like a rectangle. Code that takes a reference to a Rectangle might not anticipate this behavior.