Witam, chciałbym utworzyć wektor, do którego mogę wsadzić wszystkie pochodne klasy Point (klasa X to klasa pomocnicza, aby móc utworzyć wektor, nie działało z samym Point bo klasa abstrakcyjna), ale przy wywoływaniu funkcji to_string wywoływała się funkcja zależna od tego jaki obiekt jest obecnie w pętli for. W jaki sposób mogę to zrobić?
#include <iostream>
#include <cstdio>
#include <vector>
using namespace std;
class Point{
public:
Point(int x = 0, int y = 0) :_x(x), _y(y){}
virtual ~Point(){}
virtual void to_string() const=0{
cout << "?";
}
int GetX() const{ return _x; }
int GetY() const{ return _y; }
void SetX(int x){ _x = x; }
void SetY(int y){ _y = y; }
protected:
int _x, _y;
};
class X: public Point{
public:
X(int x=0,int y=0) :Point(x,y){}
void to_string() const {
cout << "X";
}
};
class Tree : public X{
public:
Tree(int x = 0, int y = 0):X(x,y){}
virtual void to_string()const {
cout << "T";
}
};
class Wall : public X{
public:
Wall(int x = 0, int y = 0) :X(x, y){}
void to_string()const {
cout << "W";
}
};
class Grass : public X{
public:
Grass(int x = 0, int y = 0) :X(x, y){
}
void to_string()const {
cout << ".";
}
};
int main(void){
Tree T(1,2);
Wall W(1,1);
Grass G(2,2);
std::vector < X > dane;
dane.push_back(T);
dane.push_back(W);
dane.push_back(G);
for (size_t i = 0; i < dane.size(); i++){
dane[i].to_string();
}
return 0;
}