Question 2:
Why is Dfs a subclass of level? Sorry, but do you comprehend what subclassing implies? It means Dfs IS a level. As that is most certainly not the case, don't do it. If you're going to use object orientation, at least do it right.
Question 3:
How do you use Nfs? As you already demonstrated that you do not really understand how object orientation works, I'd dare to say that you just do it wrong.
Um, sorry but my answers were simply based on what I saw. I saw a pretty messy setup, that is using inheritance when the appropitiate approach would be composition. That wasn't "rude", I was simply pointing out that the whole initial situation is pretty messy.
If you still think that anything I wrote was "rude", please point out the parts in which I did not respond appropitiately. I try to be objective and helpful, apparently I am creating a false impression of what I am trying to say. Even if I knew you were an university student, that wouldn't have changed my response.
Now, let's go back to the topic at hand:
For the whole thing to work as intended, you'd have to create your dfs object with it's copy constructor, the argument being the level object- that's what I meant when I asked how you use Nfs. Normally you'd write an own copy constructor here to avoid the both objects referring to the same array, but apparently in this set up this appears to be the goal - to go back to my previous statement, this is simply not object oriented programming, this is a hack that uses unintended side effects of C++ subclasses. Which really makes me question the point of having a dfs class in the first place.
The solution to your problem is that you probably created a new dfs object out of the blue rather than using the copy constructor, though of course without seeing your actual code I can't say for sure.
the subclass is unable to access the superclass adjacencyMatrix[i][j] and as such returns false data
That will be a compiler error, not a wrong data. You have a matrix in your derived class (in protected ambit), and you can access that.
Where do you ever fill that adjacent matrix?
I tested the memory addresses between the level and Dfs objects calling to find the memory address at adjacencyMatrix[i][j] and they gave different addresses
How did you test that? Different objects will have different address (even with the same class), the pointer is not shared.
That makes me wonder, what do you have an level object? Why don't just use the derived class?
Could you please post the caller code? (the main function)
About the design. I agree with hanst99, a traverse is not a level.
Consider this
1 2 3 4 5 6 7 8 9 10 11 12 13 14
class maze{
//...
graph g; //your adjacent matrix
public:
std::list<int> planPath(int start, int goal, traverse &T);
};
class traverse{
//...
public:
virtual std::list<int> reach(int start, int goal, const graph &g) = 0;
};
class depth_first_search : public traverse{/**/};
class breath_first_search : public traverse{/**/};
class right_hand_in_wall : public traverse{/**/};
Now you could change the mode that you traverse at runtime, just selecting the proper object that will perform the task.
Also:
1 2 3 4 5 6 7 8 9 10 11
bool canMove(int x, int y){ //don't be afraid of whitespace
if ( (adjacencyMatrix[x][y]) == true) {
returntrue;
}
else {
returnfalse;
}
}
bool canMove(int x, int y){
return adjacencyMatrix[x][y]; //clearer, don't you think?
}
I am pretty sure he said his professor forced him to use that setup. I don't know if using that behaviour was enforced by the professor as well, but what Chazz seems to want to do is, create an instance of Dfs, call e.g. dfsIterative, and let that sort the level the dfs was created with. Of course that is breaking all the principles of object orientation, and is basically just implementing a composition with inheritence (seriously, you'd have to be one hell of a confused hacker to think of something like that), but if that's the way Chazz wants or has to do it, why not.
On a side note, is anyone going to ban spoon licker anytime soon? You know, if one has one or two troll posts I guess you can let him pass, but he is taking this a bit too far.