mardi 24 juillet 2018

C++ Virtual Void vs no virtual

I'm confused about virtual functions. I was told that virtual in parent class means I can override it in child class. However if I omit the virtual in parent class, I'm still able to override it.

#include <iostream>
using namespace std;

class Enemy{
  public:
    //if I remove virtual, it still gets overriden in child class
  virtual void attack(){ 

    cout << "Attack Enemy!" << endl;
  }
};


class Minion{
  public:
  void attack(){
    cout << "Attack Minon!" << endl;
  }
};
int main() {
  Minion m;
  m.attack();
}

Aucun commentaire:

Enregistrer un commentaire