here my code
now my problem i wonder if you can do something to access
specific method from each object
example i want to access from Dalek class his method but i cant do this
"alien[i]->" will only access method from class ExtraTerrestre
any help will be appreciate sorry for my english

> 
> 
>     
>     
>           srand((int)time(NULL));
>           ExtraTerrestre *alien[15];
>         
>           for(int typeA=0;typeA<7;typeA++) // first alien
>           {
>               alien[typeA] = new Dalek(7,200);
>               alien[typeA]->resetPositionAlien()
>           }
>           for(int typeB=7;typeB<15;typeB++) // second alien
>           {
>               alien[typeB] = new Cylon(6,200); 
>               alien[typeB]->resetPositionAlien();
>           }
>         
>           
>           UIKit::framework(0,0,nbCol-1,nbLine-1,FOREGROUND_BLUE+FOREGROUND_INTENSITY);
>         
>           while(true)
>           {
>         
>               if(delayer > rand()%200 && i<=14)
>               {
>                   UIKit::color(FOREGROUND_GREEN+FOREGROUND_INTENSITY);
>                   alien[i]->putAlien();
>                   i++;
>               }
>            }
>     
> 

Recommended Answers

All 2 Replies

If it is a method that any kind of "ExtraTerrestre" can implement in some way (as it seems from your code), then that method should be a virtual method of the base class (ExtraTerrestre) and overridden in each derived class (Dalek and Cylon). As so:

class ExtraTerrestre {
  public:
    // ... other stuff.

    virtual void putAlien() = 0;  // a 'pure' virtual function.
};

class Dalek : public ExtraTerrestre {
  public:
    // ... other stuff.

    void putAlien();
};

void Dalek::putAlien() {
  // implementation here.
};

class Cylon : public ExtraTerrestre {
  public:
    // ... other stuff.

    void putAlien();
};

void Cylon::putAlien() {
  // implementation here.
};

Then, you will be able to call the "putAlien" function on all ExtraTerrestre objects, and it will be dispatched to the correct implementation in the derived class.

If, on the other hand, it is not a function that any ExtraTerrestre can implement, then you should either create another base-class for that purpose (e.g., "PutableAlien" or something like that) that would contain the virtual method in question, or you can use a dynamic_cast operator to cast the object pointer to the class which has the method in question.

Oh.. I just realized that you might be talking about the "resetPositionAlien()" function that is called after constructing the objects. For that, you can simply do this:

       {
           Dalek* new_alien = new Dalek(7,200);
           new_alien->resetPositionAlien();
           alien[typeA] = new_alien;
       }

[edit]^^^ exactly as Mike posted above.

The best way to do that is to create a virtual function in ExtraTerrestre class named putAlien() then override it in each of the derived classes. With that, line 28 in the code you posted will call the correct function in the derived class.

class ExtraTerrestre
{
   ...
   virtual void putAlien() = 0; // pure virtual function
}


class Dalek : public Extraterestial
{
    ...
    virtual void putAlien()
    {
      // implementation here
    }
}
Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.