3

I made a class Position, it has a tuple inside of it (the position) and some functions. I use this class in most of my other classes. Now i want to create a child class MovingPosition which shall add some functions, but no new variables. Now i want to use my MovingPosition class at the same occasions i used my Position class before.

This is a theoretical question to increase my knowledge, i don't really need it for an active project of mine since i can (and in this project i currently want to) code around that problem.

Lets say i got my two classes:

class Position{
   public:
      tuple<int,int> pos;
   ...
};
class MovingPosition : public Position{
   ...
};

and lets say i have a function:

printObj(Position pos);

and now i want to do this:

MovingPosition mPos;
printObj(mPos);

Can i just use my MovingPosition var at the same place i ask for a Position var? And if i can't is there a way to do just that?

Toastbot
  • 77
  • 9
  • 3
    MovingPosition is derived from Position and you are accepting the base type therefore yes, you can use a derived class when the base class is accepted, its called PolyMorphism. – SPlatten Aug 21 '19 at 13:15

0 Answers0