| | |
Define abstart overridable method
Please support our C++ advertiser: Intel Parallel Studio Home
![]() |
•
•
Join Date: Apr 2009
Posts: 4
Reputation:
Solved Threads: 0
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.
Your help would be greatly appriciated, i am eager to learn how to accomplish this.
C++ Syntax (Toggle Plain Text)
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 } };
C++ Syntax (Toggle Plain Text)
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; }
You should read up on virtual methods.
Hope this helps.
C++ Syntax (Toggle Plain Text)
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"); } };
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:
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
header for derived class:
Derived.cpp:
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.
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.
So perhaps you need to change the signature of your isKnownWord function to:
C++ Syntax (Toggle Plain Text)
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
C++ Syntax (Toggle Plain Text)
class Base { public: virtual bool isKnownWord(){}; };
C++ Syntax (Toggle Plain Text)
#include "Base.h" class Derived:public Base { public: virtual bool isKnownWord(); };
C++ Syntax (Toggle Plain Text)
#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.
C++ Syntax (Toggle Plain Text)
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!
Those who understand binary .....
And those who don't!
![]() |
Similar Threads
- Starting Python (Python)
- Polymorphism/classes (C++)
- From Java beginner:Am I right with answers?:) (Java)
- Store and Retrieve Information (Java)
- Help regarding rectangle moving (Java)
- I want to learn - How to create and use objects. (C#)
- Callback inside wrapper class dilemma (C++)
- How to run tasks with priority? (Java)
Other Threads in the C++ Forum
- Previous Thread: Executable only opens on my computer :/
- Next Thread: system("CD"). Help please :(
| Thread Tools | Search this Thread |
api array based binary c++ c/c++ calculator char char* class classes code coding compile console conversion count database delete deploy desktop developer directshow dll download dynamic dynamiccharacterarray email encryption error file forms fstream function functions game givemetehcodez google graph gui homeworkhelp iamthwee ifstream input int integer java lib linkedlist linker linux list loop looping loops map math matrix memory multiple news number numbertoword output parameter pointer problem program programming project python random read recursion recursive reference return rpg sorting string strings struct temperature template templates test text text-file tree unix url variable vector video visualstudio win32 windows winsock wordfrequency wxwidgets






