calling method of stored inherited class object

Hi All, I've been banging my head against the wall for a while on this.
I have a parent class and bunch of child classes. I want to store a instance of the child class in another class, then call the child class function. but it keeps using the parent. I've tried passing the child into the container by reference and it doesn't work either..
Here's the details.

In my library header file I have

class Parent {
	public:
		int get();
};
class ChildA : public Parent {
	public:
		int get();
};
class Container {
	public:
		Parent par;
		Container( Parent P );
		int doit();
};

In my library cpp file i have

int Parent::get(){ return 0; };
int ChildA::get(){ return 1; };

Container::Container(Parent P){
	par = P;
};
int Container::doit() {
	return par.get();
};

in my arduino ino file in setup() i have

ChildA A = ChildA();
Container C = Container( A );
Serial.print( C.doit() );

The value that I get is 0.
When i want to get 1.

I'm somewhat new to c++ so hopefully this is something easy?
My googling for a solution has failed me to, so i must not be using the right words.

Thank you!

He's what I would do:

class Parent {
  public:
    virtual int get();
};

class ChildA : public Parent {
  public:
    virtual int get();
};

class Container {
  public:
    Container( Parent &P );
    int doit();

  private:
    Parent *ptr;
};

int Parent::get() {
  return 0;
};

int ChildA::get() {
  return 1;
};

Container::Container(Parent &P) : ptr(&P) {
};

int Container::doit() {
  return ptr->get();
};


void setup() {
  ChildA A;
  Container C(A);
  
  Serial.begin(115200);
  delay(1000);  
  Serial.print(C.doit());
}

void loop() {}

Your object A is both a Parent, and a ChildA. Your Container expects an argument of type Parent, so when you pass object A, Container "sees" the object as type Parent. If you want a Child method to over-ride a Parent method, even when a ChildA object is referenced, you much define the Parent method as virtual.

Google "c++ virtual methods". There are plenty of good tutorials out there that explain the finer points.

Regards,
Ray L.

@gfvalvo - That all worked great!! Thank you! ...and I understand everything except this

Container::Container(Parent &P): ptr(&P){

It seems like that's assigning the pointer, but it's syntax I don't know. Does it have a name that I could read more about?

@rayLivingston - thanks for the tip. Making those function virtual alone didn't work, but I'll use that and the other answer to go learn about virtual and passing pointers! Thanks for the *pointer.

Container::Container(Parent &P)

The constructor for the Container class takes as its parameter a REFERENCE to a Parent object. Google - 'c++ reference type'

: ptr(&P)

The address of the REFERENCE (and hence the address of the argument passed) is assigned to the pointer. Google - c++ initializer list.

Below are 2 other methods I can think of. There may be more:

class Parent {
  public:
    virtual int get();
};

class ChildA : public Parent {
  public:
    virtual int get();
};

class Container {
  public:
    Container( Parent *P );
    int doit();

  private:
    Parent *ptr;
};

int Parent::get() {
  return 0;
};

int ChildA::get() {
  return 1;
};

Container::Container(Parent *P) : ptr(P) {
};

int Container::doit() {
  return ptr->get();
};


void setup() {
  ChildA A;
  Container C(&A);
  
  Serial.begin(115200);
  delay(1000);  
  Serial.print(C.doit());
}

void loop() {}
class Parent {
  public:
    virtual int get();
};

class ChildA : public Parent {
  public:
    virtual int get();
};

class Container {
  public:
    Container( Parent &P );
    int doit();

  private:
    Parent &parentRef;
};

int Parent::get() {
  return 0;
};

int ChildA::get() {
  return 1;
};

Container::Container(Parent &P) : parentRef(P) {
};

int Container::doit() {
  return parentRef.get();
};


void setup() {
  ChildA A;
  Container C(A);
  
  Serial.begin(115200);
  delay(1000);  
  Serial.print(C.doit());
}

void loop() {}