abcd12344445555
Programmer
Code:
class Parent
{
public:
virtual void functionA() = 0;
virtual void functionB() = 0;
};
class SonA : public Parent
{
public:
//implements virtual methods
void functionA(); //...
void functionB(); //...
//SonA's exclusive method
void exclusiveMethodSonA();
};
class SonB : public Parent
{
public:
//implements virtual methods
void functionA(); //...
void functionB(); //...
//SonB's exclusive method
void exclusiveMethodSonB();
};
I have an array of Parent* and at any given position I can instantiate either a SonA or a SonB object:
EXAMPLE:
Code:
int main(int argc, char *argv[])
{
Parent* array[10];
array[0] = new SonA();
array[1] = new SonB();
array[2] = new SonA();
array[3] = new SonA();
array[4] = new SonB();
//...
}
Given the scenario above, how can I access SonB's exclusive method? Something like: array[4]->exclusiveMethodSonB() wont work.
Do I have to create an interface with virtual methods in the Parent class, even for methods that wont be implemented in SonB and vice-versa?
Thanks.