Abstract Class
From qtnode
An Abstract Class is a class that cannot be implemented directly but must be inherited by a superior class. An abstract class is created by creating at least one virtual member function with a 0 (zero) initializer. Consider the following code:
class AbstractClass
{
AbstractClass(void);
virtual void DoSomething(void) = 0;
};
class SuperiorClass : public AbstractClass
{
SuperiorClass(void);
void DoSomething(void);
};
In the class AbstractClass, there is a virtual member function definition followed by an "= 0". What that effectively does is cause the compiler to force the programmer to inherit this class into a superior class (called SuperiorClass in our example) and implement that virtual function. The purpose of such a declaration is to provide a place holder for a particular method, in this case DoSomething(), where the exact implementation at this level (the abstract class level) is not yet known. The function MUST be virtual so that it is possible for the superior class to implement (override) it. This is what is known as a 'pure virtual' function.
Where this comes in handy in building an OOP (object oriented programming) application is it allows you to build a core-library of objects that do nothing other than set up an environment of "known functionality" which is to be implemented later by some (now unknown) objects. Consider the following code:
class Animal
{
Animal(void);
virtual QString speak(void) = 0;
};
class Dog : public Animal
{
Dog(void);
QString speak(void) { return "woof"; }
};
class Cat : public Animal
{
Cat(void);
QString speak(void) {return "meow"; }
};
void MakeSomeNoise(Animal &animal)
{
qDebug( animal.speak() );
}
void Test(void)
{
Dog dog;
Cat cat;
MakeSomeNoise(dog);
MakeSomeNoise(cat);
}
In this example, there is an abstract object 'Animal' which knows that it needs to be able to speak, but it also knows that it doesn't know how '=0'. Then there are superior classes, Dog and Cat, that take care of implementing how speak() is implemented. Where this is a real benefit is that now a whole library of code can be written, like MakeSomeNoise(), on the base abstract class Animal, without actually knowing what kind of Animal it is dealing with. For that matter, it doesn't even care. All MakeSomeNoise() cares about is that it DID receive an Animal as its parameter value... and that is something the compiler takes care of for you.
The point of an abstract class, then, is to set up an environment, or skeleton, for implementation later. The implementation later may include things not envisioned by the original abstract class, but that won't matter. In some cases, the abstract class would have to be modified to include additional skeleton framework (pure virtual functions), or the superior class will simply take care of the implementation details directly. A well-written abstract class won't require much modification once it is complete, and will offer terriffic flexibility to the superior classes that use it.