I am currenty doing a Sudoku game in c++. For that , I defined many classes including,
class Pos
for position, and class Error
for maintaining errors.
Now, in class Pos
,
class Pos {
int x;
int y;
public:
Pos();
Pos(Pos const&);
Pos(int,int);
void setPos(int,int);
void setx(int);
void sety(int);
int getx() const ;
int gety() const ;
string getPosReport();
virtual ~Pos();
};
What I have done:
I have restricted the value for x and y to lie between 0-9 by writing setx
and sety
as,
void Pos::setx(int x){
if((x < 0) & (x > 9))
throw Exception("Invalid Position");
else
this->x = x;
}
void Pos::sety(int y){
if((y < 0) & (y > 9)){
throw Exception("Invalid Position");
}
else
this->y = y;
}
My Problem: How Pos()
must be defined.
What I have worked:
Removing default constructor, didnt helped me because,
class Error { string errmsg; Pos pos1,pos2; // Pos() must be called!! default constructor cannot be removed! bool isError; // ...... }
I can initialize x and y to -1 in
Pos()
, but I feel that will bring instability in my code.I can have
int* x
andint* y
inclass Pos
such that I can have x = y = NULL, but I feel Pointers will make my code relatively complex than now.so,it will be better if some one gives a solution which stimulates the scenario like x = y = N/A (not applicable)