Any scenario where you need a common interface.
Just making this example up, may be better ones. It can create and print a binary tree. The CNode class stores the subtrees and can call the Print method the same way whether it is a node or a leaf.
Code:
#include <iostream>
class CBase
{
public:
virtual void Print() = 0;
};
class CNode : public CBase
{
public:
CNode(CBase* NewLeft, CBase* NewRight)
{
Left = NewLeft;
Right = NewRight;
}
virtual void Print()
{
std::cout << "(";
Left->Print();
std::cout << ", ";
Right->Print();
std::cout << ")";
}
protected:
CBase* Left;
CBase* Right;
};
class CLeaf : public CBase
{
public:
CLeaf(float NewValue)
{
Value = NewValue;
}
virtual void Print()
{
std::cout << Value;
}
protected:
float Value;
};
int main()
{
CBase* BinaryTree = new CNode(new CLeaf(3.0f), new CNode(new CLeaf(1.5f), new CLeaf(9.2f)));
BinaryTree->Print();
return 0;
}
Haven't tested it so it may contain errors (and does contain memory leaks).