I am trying to define an abstract overridable method isKnownWord(), i am recieving errors but i cannot see where they are coming from. Can any one enlighten me.

public:
  spellChecker() {
    wordList=NULL;
  }
 
  void addWord(char* aWord) {
    int word_size=strlen(aWord) + 1;
    char* dest=new char[word_size];

    strcpy(dest,aWord);
    wordRecord* newRec=new wordRecord;
	newRec -> word=dest;
	newRec -> next=wordList;
	wordList=newRec;
  }

  void printDict() {
	wordRecord* wordPointer = wordList;
    while(wordPointer != NULL) {
    	printf("%s\n", wordPointer -> word);
    	wordPointer = wordPointer -> next;   
    }
  }
  public override bool isKnownWord() { // needs to be abstract and overridable
  }
};
int main() {
  char* words[] = {(char*)"hello", (char*)"Womble", (char*)"goodbye"};

  char inputWord[80];

  spellChecker * sc = new caseSensitiveSpellChecker();
   
  for (int i=0; i<sizeof(words)/sizeof(char*); i++)  sc->addWord(words[i]);

  printf("Case sensitive dictionary contents:\n");
  sc->printDict();

  printf("Enter word ('END' to end):  ");
  scanf("%s", inputWord);

  while (strcmp(inputWord, "END")) {
    if (sc->isKnownWord(inputWord)) printf("yes\n");
    else printf("no\n");
    printf("Enter word ('END' to end):  ");
    scanf("%s", inputWord);
  }

  delete sc;

}

Your help would be greatly appriciated, i am eager to learn how to accomplish this.

You should read up on virtual methods.

class Greeting
  {
  public:
    // Here is our abstract, overridable method
    virtual std::string hello() const = 0;
  };

// Here is an example of how it is used
class Saludo: public Greeting
  {
  public:
    std::string hello() const { return std::string("Buenos Dias"); }
  };

Hope this helps.

In standard C++, to make a function overridable, you'd make it virtual in your base class.

So perhaps you need to change the signature of your isKnownWord function to:

public:
        virtual bool isKnownWord(){};

Then in any derived classes you can override the function, all you'd do is declare it as a virtual function in the header for your derived class and then define the overridden function in the .cpp implementation.

e.g.
header for base class

class Base
{
    public:
        virtual bool isKnownWord(){};
};

header for derived class:

#include "Base.h"
class Derived:public Base
{
    public:
        virtual bool isKnownWord();
};

Derived.cpp:

#include "Derived.h"

bool Derived::isKnownWord()
{
     // your overridden code here
}

If you're targeting the CLR then I think you should use the override keyword in the header file for your derived class.
i.e.

virtual bool isKnownWord() override;

I don't think that the override keyword is a part of standard C++....But I could be wrong, I'm still using VS2003!

Cheers for now,
Jas.

This article has been dead for over six months. Start a new discussion instead.