943,650 Members | Top Members by Rank

Ad:
  • C++ Discussion Thread
  • Unsolved
  • Views: 492
  • C++ RSS
Aug 7th, 2009
0

Define abstart overridable method

Expand Post »
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.

C++ Syntax (Toggle Plain Text)
  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. };

C++ Syntax (Toggle Plain Text)
  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.
Similar Threads
Reputation Points: 10
Solved Threads: 0
Newbie Poster
MV89 is offline Offline
4 posts
since Apr 2009
Aug 7th, 2009
0

Re: Define abstart overridable method

You should read up on virtual methods.
C++ Syntax (Toggle Plain Text)
  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.
Featured Poster
Reputation Points: 1140
Solved Threads: 229
Postaholic
Duoas is offline Offline
2,039 posts
since Oct 2007
Aug 7th, 2009
0

Re: Define abstart overridable method

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:
C++ Syntax (Toggle Plain Text)
  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
C++ Syntax (Toggle Plain Text)
  1. class Base
  2. {
  3. public:
  4. virtual bool isKnownWord(){};
  5. };
header for derived class:
C++ Syntax (Toggle Plain Text)
  1. #include "Base.h"
  2. class Derived:public Base
  3. {
  4. public:
  5. virtual bool isKnownWord();
  6. };
Derived.cpp:
C++ Syntax (Toggle Plain Text)
  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.
C++ Syntax (Toggle Plain Text)
  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.
Reputation Points: 590
Solved Threads: 123
Practically a Master Poster
JasonHippy is offline Offline
672 posts
since Jan 2009

This thread is more than three months old

No one has posted to this discussion for at least three months. Please let old threads die and do not reply to them unless you feel you have something new and valuable to contribute that absolutely must be added to make the discussion complete. Otherwise, please start a new thread in this forum instead.
Message:
Previous Thread in C++ Forum Timeline: Executable only opens on my computer :/
Next Thread in C++ Forum Timeline: system("CD"). Help please :(





About Us | Contact Us | Advertise | Acceptable Use Policy
Forum Index | Build Custom RSS Feed


Follow us on Twitter


© 2011 DaniWeb® LLC