Let's say that I have class named MasterClass that has some children like ChildA, ChildB etc with public inheritance.
Also MasterClass has a var called MVar that is from type AnimalClass and obviously there are some children like DogClass, CatClass etc
MasterClass has that AnimalClass but ChildA has DogClass, ChildB has CatClass etc.
How I can make a function that is defined only in master but uses the inherit one throught its child I mean:
ChildA ca; ca.emitSound();
and emitSound just calls something like MVar.makeSound();
Where emitSound is only defined on AnimalClass and its behaviour depends on child's type.
My problem is that it seems that emitSound is always calling AnimalClass emitSound not DogClass emitSound.
Obviously if I define a emitSound in ChildA, ChildB etc it works fine but I just wanna make the code smaller.
Of course my real life issue is more complex that this stupid example but I think that the problem is pretty much the same, ie. emitSound could be a virtual one to allow some children to overwrite its behaviour etc.
Regards!
CODE:
AnimalClass 
{
public:
      AnimalClass();
      virtual void makeSound(){ printf("nosound";}        
}
DogClass: public AnimalClass 
{
public:
      DogClass();
      void makeSound(){ printf("bufff";}        
}
MasterClass 
{
public:
      MasterClass();
      AnimalClass *ani;
      void emitSound(){ani->makeSound();}     
}
ChildAClass: public MasterClass
{           
public:
      ChildAClass(){ani=new DogClass();}
      DogClass *ani;
}
main()
{
    ChildAClass c;
    c.emitSound();
}
The problem is that it prints "nosound" instead "bufff"

