Virtual functions
They get resolved at run-time, leading to a shorter compile-time and less binary code.
#include <iostream>
class Animal
{
public:
virtual void makeSound() = 0;
};
class Cow : public Animal
{
public:
void makeSound() { std::cout << "Moo" << std::endl; };
};
int main()
{
Cow cow;
cow.makeSound();
return 0;
}
Templates
They get resolved at compile-time, leading to a shorter run-time and more binary code.
#include <iostream>
template <typename T>
class Animal
{
public:
void makeSound() { t.makeSound(); };
private:
T t;
};
class Cow
{
public:
void makeSound() { std::cout << "Moo" << std::endl; };
};
int main()
{
Animal<Cow> cow;
cow.makeSound();
return 0;
}