c++ class inheritence
Posted: Wed Sep 19, 2007 5:59 am
The inheritance concept has confused me a bit although i think i more or less understand it now.
This is just some code, what it is for is not important, what i need to know is if i have got the inheritance part right.
1. class King should be and exact copy of class Piece as it contain nothing more than the constructor call. Is this correct?
2. I have tried different ways of writing class King's constructor, as it should be inherited automaticly (or so i thought) from the base class. This was seems to be the only thing that works. I have a hard time accepting that the constructor cant just be inherited.
3. Is there anyway to prevent other "users" of class Piece than class King?
4. other comments and stuff are welcome as usual, but im not interested in a long discusion, i just want to know if i got it right.
Thanks
Code: Select all
typedef unsigned long long uint64;
class Piece {
const bool Color:1;
unsigned char Pos:6;
public:
Piece(const bool b,const unsigned char x,unsigned const char y):Color(b),Pos(x + (y * 8)) {}
uint64 getpos() const {return 1ull<<Pos;}
bool move(const char x,const char y) {
const char test = x + (y * 8) + Pos;
if(test >= 0 && test < 64) {
Pos = test;
return 0;
}
else return 1;
}
};
class King : public Piece {
public:
King(const bool b,const unsigned char x,const unsigned char y) : Piece(b,x,y) {}
};
1. class King should be and exact copy of class Piece as it contain nothing more than the constructor call. Is this correct?
2. I have tried different ways of writing class King's constructor, as it should be inherited automaticly (or so i thought) from the base class. This was seems to be the only thing that works. I have a hard time accepting that the constructor cant just be inherited.
3. Is there anyway to prevent other "users" of class Piece than class King?
4. other comments and stuff are welcome as usual, but im not interested in a long discusion, i just want to know if i got it right.
Thanks