Pure Virtual Function

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
I make this Class. But I don't know how to implement this function from Header file to its Class File !!!

Header File 
[code]
  #pragma once
class IBullsAndCowsView
{
	public:
		virtual ~IBullsAndCowsView() {}
		virtual int guess() = 0;
		virtual void showGuess() = 0;
		virtual void startNewGame() = 0;
		virtual void endGame() = 0;
		virtual void run() = 0;
	}; 




guess:
int lNumberOfGuesses = 0;
string = lGuess;
do {
cout << "Make a guess: ";
formatted input to lGuess;
call game model method guess using lGuess as parameter;
lNumberOfGuesses +=1;
call view method showGuess;
}while call game model method getBulls and test result to be not equal to 4;
return lNumberOfGuesses;
• showGuess:
This method prints the number of bulls and cows guessed. This information is available
in the game model. Compare with the solution of problem set 3. We will need to use the
model’s getter methods.
• startNewGame:
This method announces that a new game has started (console output) and starts a new
game by calling the game model method start.
• endGame:
This method has to update the game state (usually a flag to signal whether the game is
still active) and output a message to indicate that the game has ended.
• run:
This is a controller method that implements the “game loop.” The run algorithm:
declare string variable lQuery
while game is active (a Boolean controller flag) do
call view method startNewGame;
call view method guess;
output to console “New game, Y/N? ”;
formatted input to lQuery;
if lQuery[0] not equal to ‘Y’ then
if lQuery[0] not equal to ‘y’ then
call view method endGame;
end;
end;
end;[/code]

And after this instruction , this is what they are saying : To implement a console view, you need to create a subclass of IBullsAndCowsView that
defines all the view methods and maintains a reference to a game model object as well as
the game state (a Boolean flag). As usual, the view class has to also define a constructor.
The view class BullsAndCowsConsoleView is defined as follow.


This is that subclass Code:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
#pragma once
#include "IBullsAndCowsView.h"
#include "BullsAndCows.h"
class BullsAndCowsConsoleView : public IBullsAndCowsView
{
private:
BullsAndCows& fGameModel;
bool fActive;
public:
BullsAndCowsConsoleView( BullsAndCows& aGameModel );
virtual int guess();
virtual void showGuess();
virtual void startNewGame();
virtual void endGame();
virtual void run();
};


I got the logic but no idea how to implement into Class .CPP file. Also some of calling method I am confused.

Thanks in Advance.
I don't know exactly what you want...
A cpp would look like this:
1
2
3
4
void BullsAndCowsConsoleView::showGuess()
{
  std::cout << guess;
}


You might use such a class like this:
1
2
3
4
5
6
7
8
9
10
11
int main()
{
  IBullsAndCowsView *bcv = NULL;
...
  bcv = new BullsAndCowsConsoleView;
...
  bcv->showGuess();
...
  delete bcv;
  return 0;
}
Topic archived. No new replies allowed.