2 Replies - 297 Views - Last Post: 24 October 2013 - 11:52 PM Rate Topic: -----

#1 k31453  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 3
  • Joined: 24-October 13

Virtual Abstract Class Implementation

Posted 24 October 2013 - 11:11 PM

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;

nd 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:
#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();
};



Link to the project :
My link


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.

Is This A Good Question/Topic? 0
  • +

Replies To: Virtual Abstract Class Implementation

#2 IngeniousHax  Icon User is offline

  • |>|20-514<|{3|2

Reputation: 78
  • View blog
  • Posts: 1,358
  • Joined: 28-March 09

Re: Virtual Abstract Class Implementation

Posted 24 October 2013 - 11:45 PM

Why are you using nothing but virtual functions? I know this isn't your question, but I'm just curious as to where your thought process is.

EDIT:: NVM, didn't see that last part.

Try overriding the functions in the inherited class. You must define your functions in the class that is inheriting the base class if they are declared virtual in a base class. (I think)

Have a gander here for some guidance: Virtual Functions and more

This post has been edited by IngeniousHax: 24 October 2013 - 11:51 PM

Was This Post Helpful? 0
  • +
  • -

#3 eker676  Icon User is offline

  • Software Engineer
  • member icon

Reputation: 378
  • View blog
  • Posts: 1,833
  • Joined: 18-April 09

Re: Virtual Abstract Class Implementation

Posted 24 October 2013 - 11:52 PM

View PostIngeniousHax, on 25 October 2013 - 02:45 AM, said:

Why are you using nothing but virtual functions? I know this isn't your question, but I'm just curious as to where your thought process is.


Interface design. (Edit: Ahh, you meant why is the base class pure-virtual. Oh well.)

Simple example with shapes:
class Shape
{
public:
  void draw() = 0;
}

class Rectangle : public Shape
{
public:
  void draw() { /* draw a rectangle */ }
}

class Triangle : public Shape
{
public:
  void draw() { /* draw a triangle */ }
}

int main()
{
  // Let's say this is the main paint loop for our application
  // We don't really want to be specializing our drawing if the
  // shape knows how to do it.

  vector<Shape*> objects { new Rectangle(), new Triangle() };

  // Every subclass of Shape has to have a draw function because
  // Shape is an abstract class.
  for( Shape* s : objects )
  {
    s->draw();
  }
}


Onto the actual question. You already have the header file for the derived class. Create a .cpp file, include the header file and implement the functions.

This post has been edited by eker676: 24 October 2013 - 11:53 PM

Was This Post Helpful? 0
  • +
  • -

Page 1 of 1