0
#include<iostream>
using namespace std;


class Enemy{

    public:
        virtual void attack(){};
        virtual void setAttack(){};
        virtual int getAttack();
};

class Ninja: public Enemy{

    private:
        int apn;
    public:
        int getAttack(){return apn;};
        void setAttack(int a){apn=a_}
        void attack(){
            cout<<"Ninja attach with"<< apn <<endl;
        };
};

class Monster: public Enemy{

    private:
        int apm;
    public:
        int  getAttack(){return apm;};
        void setAttack(int b){apm=b;}
        void attack(){
            cout<<"Monster attack with"<< apm << endl;
        };
};

int main(){

    Ninja n;
    Monster m;
    n.setAttack(2);
    m.setAttack(3);
    n.attack();
    m.attack();

    return(0);

im trying to initialize get and set functions as a virtual function but my code does not work?

user4581301
  • 33,082
  • 7
  • 33
  • 54
  • 1
    Overriding functions hav to have the same signature as the function they override. In `Enemy`, change `setAttack()` to `setAttack(int)`. – Pete Becker Dec 09 '19 at 01:27
  • Missing the close brace in `main`. Couple mystery underscores show up. – user4581301 Dec 09 '19 at 01:29
  • 1
    Note that if you had used the keyword `override` after `setAttack` in a derived class, your compiler would inform you it isn't currently overriding anything which should help clue onto the issue – Tas Dec 09 '19 at 01:30
  • Side tip: it's unsafe in general to use virtual methods without also adding a virtual destructor. Also, making the base methods pure virtual (= 0) will make the compiler yell at you if you don't override them correctly, which might catch this. – parktomatomi Dec 09 '19 at 01:30
  • @parktomatomi — there’s no problem calling virtual functions when the base class does not have a virtual destructor. When the base class does not have a virtual destructor, the behavior of a program is undefined if it deletes an object of a derived type through a pointer to the base. – Pete Becker Dec 09 '19 at 02:42
  • @PeteBecker that's true, and there are no nontrivial data members in these example classes anyway. I should be more specific about that advice. But it's still a good _practice_ to go ahead and push anyway for beginners using virtual methods, because it's pretty likely to wind up there at the end and get leaks (monster factory, array of NPCs, etc) than to not need it. Virtual code missing virtual destructors is way more likely to be because the writer doesn't know about that UB than because they're being judicious and minimal. – parktomatomi Dec 09 '19 at 09:20
  • @parktomatomi -- again: the behavior of a program is undefined if it deletes an object of a derived type through a pointer to a base type that does not have a virtual destructor. There is nothing there about "nontrivial data members". – Pete Becker Dec 09 '19 at 14:43
  • Got it, thanks for the correction – parktomatomi Dec 09 '19 at 15:59

1 Answers1

0

I solved problem with this question.

and I changed my Enemy class with this

class Enemy{

public:
    virtual void attack(){};
    virtual void setAttack(){};
    virtual int getAttack()=0;
    };