What are the uses of pure virtual functions in C++?

patricksweeney picture patricksweeney · Jul 1, 2009 · Viewed 38.4k times · Source

I'm learning about C++ in a class right now and I don't quite grok pure virtual functions. I understand that they are later outlined in a derived class, but why would you want to declare it as equal to 0 if you are just going to define it in the derived class?

Answer

Steven Sudit picture Steven Sudit · Jul 1, 2009

Briefly, it's to make the class abstract, so that it can't be instantiated, but a child class can override the pure virtual methods to form a concrete class. This is a good way to define an interface in C++.