If you do something like:
class Base {
public:
Base(){}
virtual void foo() = 0;
};
void Base::foo() {
{
cout << "Hello";
}
you will get an abstract class, so you will not be able to create an object b of class Base and have it use your implementation of foo() by invoking b.foo().
So this is not what you want! The next question is why were you allowed to provide an implementation for Base::foo if you cannot ever use it. Well, actually you can! Not directly though.
class Deriv : public Base {
public:
Deriv(){}
virtual void foo();
};
void Deriv::foo()
{
Base::foo(); cout << " world!";
}
As you can see Base::foo() can be used by the derived classes.
Back to your question. The answer is you can do exactly what you specified, except that the Base class will be abstract.
The alternative is to do what most people do and not specify =0 when you give an implementation for foo() in Base. This means that derived classes can override foo() but do not have to. This actually seems to me to make perfect sense: in derived classes for which Base::foo()'s default implementation makes sense, you keep it, but for those that need something different you override it.
Update: I tried to come up with a scenario that actually requires what the questions is asking about and came up with this:
Imagine that Document and its derived classes represent document types and that Document contains a virtual method getClassIcon() returning the URL for the image file for the icon that represents the document type. It makes sense for Document to have an icon (something representing a generic document) and it also makes sense to require all derived classes to override getClassIcon() in order to provide their own icons.
Having said that, in this particular example it seems easy to find an alternative design: make Document::getClassIcon() a pure virtual method (=0) (making Document an abstract class) and add OtherDocument as one of its derived classes, giving it the implementation of getClassIcon Document had previously. PDFDocument, MSWordDocument etc will all be required to override getClassIcon() as required. What this re-design might be taken to reveal is that the previous design (supported by an imaginary version of C++ that allows what the question is asking for) had conflated the role of the base class, Document, and the role of a class standing for the documents that do not belong to any of the known types, OtherDocument. It is all in the eye of the beholder of course :-) One may argue that the second design was merely necessary because of functionality missing from the language. I guess we think (to a large extent) according to the (computer) language we speak. :-)