Reputation: 2349
This is similar to other questions I'm sure, I read through. I am trying to write a move class. I need a Player class and an Item class that inherits the move class, or vice versa. That is what I am having difficulty with. I can't wrap my head around, or get to work, a way that the base class is not "Move." I'm lost here...
class Player {
protected:
int x;
int y;
int str;
int speed;
int level;
int exp;
public:
Player(int _str, int _speed, int _level, int _exp) { x=0;y=0;str=_str;speed=_speed;level=_level;exp=_exp; }
int GetX() {return x;}
int GetY() {return y;}
int GetStr() {return str;}
int GetSpeed() {return speed;}
int GetLevel() {return level;}
int GetExp() {return exp;}
};
class Move : public Player {
public:
void TryMove(int n) { x += n; }
};
int main(int argc, char *argv[])
{
Player You(101, 5, 3, 43625);
//You.TryMove(5); how?
}
TryMove(5) fails. If I do it the other way, then they type is then Move (?) and that doesn't sound right at all...
Upvotes: 0
Views: 123
Reputation: 2313
The way that I would recommend thinking of it is the Is A
or Has A
idiom. So... Is the Player a Move? Probably not, Is the Move A Player? also probably not. Then lets try the Has A
. Does The Move Have a Player or Does the Player Have A move? I would Say that the Player Has a Move Personally. This would mean not using inheritance but rather having the player contain an instance of the Move
class. So...
class Player{
public:
Move myMove;//where move is defined already as whatever you need it to be.
};
//then your main
int main(int argc, const char** argv){
//...other setup here
Player p;
p.myMove.TryMove(10);
//whatever else...
}
This is how i might approach your design. As far as the error... In the code above you had Move
inherit from Player
but you created a Player
and expected it to have the functionality of Move
but it would have no way of having that functionality based on the inheritance you set up in the example code.
Let me know if you need any clarification of what I have said or anything else. Good Luck
EDIT:
Based on your comment I would suggest that you use a wrapper function that will get the value that you need.
class Player{
public:
void TryMove(int i);
private:
Move myMove;
int x;//the value you will be getting from Move::tryMove
};
void Player::TryMove(int i){
this->x = myMove.tryMove(i);//store the value of the function call
//the function obviously won't be a void function in this case
}
There are other ways you could do this but this way is simple. If you are set on using inheritance to solve your problem I would have Player
inherit from Move
, But I still stand by my original answer I just wanted to help explain further.
Upvotes: 2
Reputation: 7625
First of all, public inheritance means IS-A relationship. So Move
IS-A Player
does not make any sense. Your design is wrong.
About your error, this is just silly. You are creating a Player
object You
and calling method of Move
on it. To use methods of class Move
, you have to create object of Move
(or any publicly derived class).
class Move : public Player {
public:
//either create a ctor that will pass params to parent ctor, or create an empty ctro in parent class
Move(int _str, int _speed, int _level, int _exp):Player(_str,_speed,_level,_exp){}
void TryMove(int n) { x += n; }
};
int main(int argc, char *argv[]){
Move You(101, 5, 3, 43625);
You.TryMove(5);
}
I would design it as following:
class Movable{
public:
virtual bool TryMove()=0;
};
class Player: public Movable {
public:
bool TryMove() override{
//implementation code
}
//other members
};
int main(){
Movable* player = new Player(101, 5, 3, 43625);
player->TryMove();
delete player;
}
Upvotes: 2