I would like to call the parent constructor like this :
Character::Character(int x, int y, int h, int s, int a, char dir) : health(h), strength(s), armor(a), direction(dir){
if(direction == 'N') {
Visual('^', x, y);
} else if(direction == 'S') {
Visual('v', x, y);
} else if(direction == 'W') {
Visual('<', x, y);
} else if(direction == 'E') {
Visual('>', x, y);
}
}
But it doesn't work well because it call the default constructor of the parent which is private
class Visual {
private:
Visual();
Visual(const Visual &);
protected:
Position coordinate;
char chara;
public:
Visual(char c, int x, int y);
};
Create a function to convert the direction into a different character, and pass that to the public constructor:
namespace { //Anonymous Namespace for functions local to your .cpp files to avoid definition conflicts
char convert_direction(char c) {
switch(c) {
case 'N': return '^';
case 'S': return 'v';
case 'E': return '>';
case 'W': return '<';
default: return '?';
}
}
}
Character::Character(int x, int y, int h, int s, int a, char dir) :
Visual(convert_direction(dir), x, y),
health(h), strength(s), armor(a), direction(dir)
{
}
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With