[Solved] C++ , variables , abstract/ virtual class


A few things. First name your functions sensible things, those names are gibberish and mean nothing to anyone on here.

Second, you’re error is quite simple. The “Wolf” and “Animal” class do not need to re-declare “inicjatywa,” so long as they declare their inheritance of “Organizmy” to be public (as they have) the ‘outside world’ will see all the public elements of “Organizmy.” The code will look like this:

class Organizmy
{
public:
    int inicjatywa;
    virtual void akcja() = 0;
    virtual void kolizja() = 0;
    virtual void rysowanie() = 0;
    virtual ~Organizmy(){};
};

class Animal: public Organizmy
{
    public:
    virtual void akcja() = 0;
    virtual void kolizja() = 0;
    virtual void rysowanie() = 0;
    virtual ~Animal(){};
};

class Wolf: public Animal
{
public:

    Wolf(){
        cout << "Crate Wolf" << endl;
        inicjatywa = 5;
    };
    ~Wolf(){};
    void akcja(){};
    void kolizja(){};
    void rysowanie(){
        cout << "W" << endl;
        cout << inicjatywa << endl; // here he output 5 
    };
};
int main()
{
    Organizmy *b;     // I create new poiter; type Organizmy
    b=new Wolf();     // He is now pointing new object Wolf
    b->rysowanie();   // here he outputs correct value of the new elemnt 
    cout<<b->inicjatywa<< endl; //but here the code outputs -8421.... 
                            //and it should be 5 not -8421....
}

2

solved C++ , variables , abstract/ virtual class