#include <iostream>
using std::cout;
using std::endl;
class CBaseClass
{
public:
virtual void whatAmI()
{
cout << "I am CBaseClass. If the Sub classes "
<< "defines a function with this signiture/n"
<< "You should never see this!" << endl;
}
void nonpolymorphic()
{
cout << "I am CBaseClass. You should always see this!" << endl;
}
// For our example this prevents an object of type
// CBaseClass from being instantiaed
private:
virtual void purevirtual() = 0;
};
class CSubClass1 : public CBaseClass
{
public:
virtual void whatAmI()
{
cout << "I am CSubClass1!" << endl;
}
void nonpolymorphic()
{
cout << "This is the nonpolymorphic all from "
<< "CSubClass1" << endl;
}
private:
virtual void purevirtual()
{
// It is now defined. Are you happy?
}
};
class CSubClass2 : public CBaseClass
{
public:
virtual void whatAmI()
{
cout << "I am CSubClass2!" << endl;
}
void nonpolymorphic()
{
cout << "This is the nonpolymorphic all from "
<< "CSubClass2" << endl;
}
private:
virtual void purevirtual()
{
// It is now defined. Are you happy?
}
};
int main(int argc, char* argv)
{
int numberOfObjects = 2;
CBaseClass* arrayOfObjects[2];
arrayOfObjects[0] = new CSubClass1;
arrayOfObjects[1] = new CSubClass2;
for (int i = 0; i < numberOfObjects; ++i)
{
cout << "The value of the variable \"i\" is "
<< i << endl << endl
<< "The next statement is: arrayOf"
<< "Objects[i]->whatAmI()" << endl;
arrayOfObjects[i]->whatAmI();
cout << endl << "The next statement is: arrayOf"
<< "Objects[i]->nonpolymorphic()" << endl;
arrayOfObjects[i]->nonpolymorphic();
cout << endl << "End of this cycle through the loop" << endl << endl;
}
cout << "I am out of the loop" << endl;
return 0;
}
</code>
My fingers just took over and banged out the C++ stuff, sorry.