I have this problem which I really struggle to even explain(as you can guess by the title) so I'll make it clear by an example
#include <iostream>
using namespace std;
class shape
{
public:
shape()
{
}
};
class triangle : public shape
{
public:
triangle()
{
}
};
class square : public shape
{
public:
square()
{
}
};
class shapeTeller
{
public:
shapeTeller() {}
void tellMeWhatShape(square s)
{
cout << "Hello, I'm a square\n";
}
void tellMeWhatShape(triangle t)
{
cout << "Hello, I'm a triangle\n";
}
void tellMeWhatShape(shape s)
{
cout << "Hello, I'm a generic shape\n";
}
};
int main()
{
shape sh;
triangle tr;
square sq;
shape shapeArray[3] = {sh, tr, sq};
shapeTeller tell;
for (auto &element : shapeArray)
{
tell.tellMeWhatShape(element);
}
}
this snippet of code prints three times "Hello, I'm a generic shape", while my desired output would be
"Hello, I'm a generic shape"
"Hello, I'm a triangle"
"Hello, I'm a square"
How can i achieve something like that, considering that I want the array to be of the superclass, and I want it to contains various subclasses?
I also want to make it clear that this is a simplified exhample but in the real implementation I can't use parametric polymorphism cause i want the shapeTeller class' methods to do completely different things.
Thanks a lot