Define abstart overridable method

Please support our C++ advertiser: Intel Parallel Studio Home
Reply

Join Date: Apr 2009
Posts: 4
Reputation: MV89 is an unknown quantity at this point 
Solved Threads: 0
MV89 MV89 is offline Offline
Newbie Poster

Define abstart overridable method

 
0
  #1
Aug 7th, 2009
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.

  1. public:
  2. spellChecker() {
  3. wordList=NULL;
  4. }
  5.  
  6. void addWord(char* aWord) {
  7. int word_size=strlen(aWord) + 1;
  8. char* dest=new char[word_size];
  9.  
  10. strcpy(dest,aWord);
  11. wordRecord* newRec=new wordRecord;
  12. newRec -> word=dest;
  13. newRec -> next=wordList;
  14. wordList=newRec;
  15. }
  16.  
  17. void printDict() {
  18. wordRecord* wordPointer = wordList;
  19. while(wordPointer != NULL) {
  20. printf("%s\n", wordPointer -> word);
  21. wordPointer = wordPointer -> next;
  22. }
  23. }
  24. public override bool isKnownWord() { // needs to be abstract and overridable
  25. }
  26. };

  1. int main() {
  2. char* words[] = {(char*)"hello", (char*)"Womble", (char*)"goodbye"};
  3.  
  4. char inputWord[80];
  5.  
  6. spellChecker * sc = new caseSensitiveSpellChecker();
  7.  
  8. for (int i=0; i<sizeof(words)/sizeof(char*); i++) sc->addWord(words[i]);
  9.  
  10. printf("Case sensitive dictionary contents:\n");
  11. sc->printDict();
  12.  
  13. printf("Enter word ('END' to end): ");
  14. scanf("%s", inputWord);
  15.  
  16. while (strcmp(inputWord, "END")) {
  17. if (sc->isKnownWord(inputWord)) printf("yes\n");
  18. else printf("no\n");
  19. printf("Enter word ('END' to end): ");
  20. scanf("%s", inputWord);
  21. }
  22.  
  23. delete sc;
  24.  
  25. }
Your help would be greatly appriciated, i am eager to learn how to accomplish this.
Reply With Quote Quick reply to this message  
Join Date: Oct 2007
Posts: 1,951
Reputation: Duoas has much to be proud of Duoas has much to be proud of Duoas has much to be proud of Duoas has much to be proud of Duoas has much to be proud of Duoas has much to be proud of Duoas has much to be proud of Duoas has much to be proud of 
Solved Threads: 214
Featured Poster
Duoas's Avatar
Duoas Duoas is offline Offline
Posting Virtuoso

Re: Define abstart overridable method

 
0
  #2
Aug 7th, 2009
You should read up on virtual methods.
  1. class Greeting
  2. {
  3. public:
  4. // Here is our abstract, overridable method
  5. virtual std::string hello() const = 0;
  6. };
  7.  
  8. // Here is an example of how it is used
  9. class Saludo: public Greeting
  10. {
  11. public:
  12. std::string hello() const { return std::string("Buenos Dias"); }
  13. };
Hope this helps.
Reply With Quote Quick reply to this message  
Join Date: Jan 2009
Posts: 306
Reputation: JasonHippy is a jewel in the rough JasonHippy is a jewel in the rough JasonHippy is a jewel in the rough JasonHippy is a jewel in the rough 
Solved Threads: 52
JasonHippy's Avatar
JasonHippy JasonHippy is offline Offline
Posting Whiz

Re: Define abstart overridable method

 
0
  #3
Aug 7th, 2009
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:
  1. public:
  2. 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
  1. class Base
  2. {
  3. public:
  4. virtual bool isKnownWord(){};
  5. };
header for derived class:
  1. #include "Base.h"
  2. class Derived:public Base
  3. {
  4. public:
  5. virtual bool isKnownWord();
  6. };
Derived.cpp:
  1. #include "Derived.h"
  2.  
  3. bool Derived::isKnownWord()
  4. {
  5. // your overridden code here
  6. }

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.
  1. 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.
There are 10 types of people in this world.....
Those who understand binary .....
And those who don't!
Reply With Quote Quick reply to this message  
Reply

This thread is more than three months old.
Perhaps start a new thread instead?
Message:


Thread Tools Search this Thread



About Us | Contact Us | Advertise | DaniWeb | Acceptable Use Policy | RSS Feed

©2003 - 2009 DaniWeb® LLC