Override parent function

To add comments, please log in or register
enrique3
47
enrique3  

Hi, is there any special syntax to override parent functions? or is not possible if they are not "virtual"?


Thanks !!

nicholi shen
2392
nicholi shen  
enrique3:

Hi, is there any special syntax to override parent functions? or is not possible if they are not "virtual"?


Thanks !!

if you are talking about "class methods" then you can overload it provided it has the same signature, however, you will not be able to call in a polymorphic way. 


 

class Animal
{
public:
   virtual void sound1() {Print("....");}
   void sound2(){Print("____");}
};

class Cat : public Animal
{
public:
   virtual void sound1()override{Print("Meow");} 
   void sound2(){Print("prrrr");}
};

... 
   Animal *cat = new Cat();
   cat.sound2();
   // "____"

   Cat *cat = new Cat();
   cat.sound2();
   //"prrr"
Anthony Garot
3286
Anthony Garot  
enrique3:

 or is not possible if they are not "virtual"?

Yeah, I think they have to be "virtual."

nicholi shen
2392
nicholi shen  
Anthony Garot:

Yeah, I think they have to be "virtual."

virtual to override (polymorphism)

otherwise you can "overload" a method with the same signature, meaning you call the method on the child object it will use child's method instead of parent's.  

Anthony Garot
3286
Anthony Garot  
Emma Schwatson:

virtual to override (polymorphism)

otherwise you can "overload" a method with the same signature, meaning you call the method on the child object it will use child's method instead of parent's.  

Oh, I see what you are saying now. And you don't have to define it as virtual in that case.

I'm used to the "virtual to override" methodology, because that is what I generally use, but it's good to know about the other.

Thanks for keeping me on my toes.

enrique3
47
enrique3  
Anthony Garot:

Oh, I see what you are saying now. And you don't have to define it as virtual in that case.

I'm used to the "virtual to override" methodology, because that is what I generally use, but it's good to know about the other.

Thanks for keeping me on my toes.

Emma Schwatson:

virtual to override (polymorphism)

otherwise you can "overload" a method with the same signature, meaning you call the method on the child object it will use child's method instead of parent's.  

But what happens if we have a method in "Animal" (parent class) calling (inside that method) to "sound2"?

Will it execute the overloaded method or the original from Animal? 

class Animal
{
public:
   virtual void sound1() {Print("....");}
   void sound2(){Print("____");}
   void methodCallingSound2(){sound2();}        
};

class Cat : public Animal
{
public:
   virtual void sound1()override{Print("Meow");} 
   void sound2(){Print("prrrr");}	// overload, not virtual override
};

... 
   Cat *cat = new Cat();
   cat.methodCallingSound2();
   // "prrr" or "____" ???
nicholi shen
2392
nicholi shen  
enrique3:

But what happens if we have a method in "Animal" (parent class) calling (inside that method) to "sound2"?

Will it execute the overloaded method or the original from Animal? 

You tell us... The code and explanations are all here... Run a test, that's how you learn.
enrique3
47
enrique3  
Emma Schwatson:
You tell us... The code and explanations are all here... Run a test, that's how you learn.

OK this is the script (commented the results):


class Animal
{
public:
   virtual void sound1() {Print("....");}
   void sound2(){Print("____");}
   void methodCallingSound1(){sound1();}        
   void methodCallingSound2(){sound2();}        
};

class Cat : public Animal
{
public:
   virtual void sound1()override{Print("Meow");} 
   void sound2(){Print("prrrr");}       // overload, not virtual override
};


//+------------------------------------------------------------------+
//| Script program start function                                    |
//+------------------------------------------------------------------+
void OnStart()
  {
//---
    Cat *cat = new Cat();
    cat.methodCallingSound1();   // Meow
    cat.methodCallingSound2();   // ____
  }
//+------------------------------------------------------------------+

So yes, we need to declare it as "virtual" if we want a real override, this is really dissapointed, because then we need to "predict" if the child classes will need some override or not... 

To add comments, please log in or register