c++, make child class know about parent class and be able to use its data

Started by
6 comments, last by andy1984 10 years, 8 months ago

Hello.

I am just programing the project i am working on.

The issue is that i seperated code like this:

In AI::ChaseUnit(...) i need to use Unit::unitDataID

I feel bad about passing it since it is one object?

Cant i do something like this->unitDataID from the AI::Chase(...) to access data from Unit:: ?

I can just pass the data to the function but as i said... i am looking for something other.

Hello.

//////////////////////// THE RAW CODE ////////////////////

//Unit.h

namespace en
{
enum UnitTypes
{
Player = 1,
Sheep = 2,
Wolf = 3,
};
};

struct unitData
{
std::string name;
en::UnitTypes type;
int hp;
int sightRange;
}

const unitDataT [] =
{
{"Player", en::Player, 999, 5},
{"Sheep", en::Sheep, 4, 4},
{"Wolf", en::Wolf, 6, 5},
};

class Unit : public AI
{
public:
Unit(Map & objMap, Textures & objTex, int unitType = en::UnitTypes::Sheep);
Unit(Map & objMap, Textures & objTex, int PosX, int PosY, en::UnitTypes unitType = en::UnitTypes::Sheep);

void LoopUnit();

int unitDataID;
int posX, posY;
sf::Sprite spr;
};

//AI.h
class AI
{
public:
AI(Map & ObjMap);

void DecideAction(int unitType);
void Execute(Unit & unit);

bool isDone;
private: //Functions
void Walk();
void Chase(UnitHolder objUniHol);
void Rest();
private:

int moveAmountX, moveAmountY;
int actionType;
int actTime;
sf::Clock clockLastActed;
//Clock how long // how long is the unit doing current action

Map & objMap;
};

Here is the raw code so sorry

Advertisement

I can't see any code. Did you use [ code ] [ /code ] (without spaces) or is the gamedev editor bummed in the gob again?

"Most people think, great God will come from the sky, take away everything, and make everybody feel high" - Bob Marley

I can't see any code. Did you use [ code ] [ /code ] (without spaces) or is the gamedev editor bummed in the gob again?

Yea i am trying to get the code to post... 7th time fail i tried so much

Are you trying to access members of class Unit (derived class) from AI::Chase (base class)? Because that is the opposite of what the title suggests ;)

You can make Chase virtual and override it in class Unit, then you know that the this pointer passed is a Unit* or more derived object.

"Most people think, great God will come from the sky, take away everything, and make everybody feel high" - Bob Marley

Are you trying to access members of class Unit (derived class) from AI::Chase (base class)? Because that is the opposite of what the title suggests ;)

Mhm, if in class A i include class B. A = base class or B = base class? i may have bad knowledge here.

You can make Chase virtual and override it in class Unit, then you know that the this pointer passed is a Unit* or more derived object.

The AI class is just container to be included in Unit class, i just want it to be separated for better visual overview.

But still then the AI code is gonna be in the Unit instead of "AI" i am trying to keep this at chunks as much as possible. Unit is for everything regarding Unit. But if there is AI, then everything from Unit that has AI should be placed in AI. At least that is what i was doing so far.

class Base

{

// stuff

};

class Derived : public Base

{

// stuff

};

Base is also known as a superclass/parent class and Derived as a subclass/child class.

"Most people think, great God will come from the sky, take away everything, and make everybody feel high" - Bob Marley

Thank you,

...

public and protected members are accesible in derived classes. private members are not.

you used private vars in AI, so they are unaccesible in Unit.

inheritance:


class Unit
{
protected:
    // vars
   
    Action intendedAction;
   
    // constructor/desctuctor
 
    // protected methods  
 
    // virtual means you will override it in derived classes, = 0 means you wont implement it here
    // you could implement a default version here if you wanted
    virtual void decideAction() = 0;
    void performAction()
    {
        // do stuff
    }
 
public:
    void tick()
    {
        decideAction();
        performAction();
    }
 
    virtual void draw() = 0;
}
class Wolf : public Unit
{
protected:
    void decideAction()
    {
        Sheep* sheep = nearestSheep();
        if (distanceToUnit(nearestSheep) <= sightRange)
        {
            intendedAction.type = chase;
            intendedAction.target = nearestSheep;
        }
    }
public:
    void draw()
    {
        // draw a wolf
    }
};
class Sheep : public Unit
{
protected:
    void decideAction()
    {
        Wolf* wolf = nearestWolf();
        if (distanceToUnit(nearestWolf) <= sightRange)
        {
            intendedAction.type = flee;
            intendedAction.target = nearestWolf;
        }
    }
public:
    void draw()
    {
        // draw a sheep
    }
}
class Player : public Unit
{
    // etc
};
 
int main()
{
    Unit* units[3];
    units[0] = new Wolf(...);
    units[1] = new Player(...);
    units[2] = new Sheep(...);
 
    for (int i = 0; i < 3;i++)
    {
        units[i]->tick();
        units[i]->draw();
    }

    // etc

    for (int i = 0; i < 3;i++)
    {
        delete units[i];
    }
}

This topic is closed to new replies.

Advertisement