| | |
Help with creating a class
Please support our C++ advertiser: Intel Parallel Studio Home
Thread Solved |
Hello everyone I have to create a class that has the following:
I would like some input if I did the above correctly.
This is what is in my header file:
This is what I have in my cpp file:
I am getting errors with what I currently have and decided to break it down piece by piece. I would like to know if what I did is correct, any input is highly appreciated.
•
•
•
•
Your Field class is the base class from which you will derive all other Field classes.
Your Field class has the following public member functions, all of which are virtual:
* void display() - does nothing here, but will display the field in derived classes.
* int edit() - does nothing here and returns 0, but will edit the field in derived classes.
* bool editable() const - returns false here, but will return the editability of the field in derived classes.
* void *data() - returns NULL here, but will return the address of data stored in the field in derived classes.
* Field *clone() const - returns NULL here, but will create a clone of the field in derived classes.
This is what is in my header file:
c++ Syntax (Toggle Plain Text)
class Field{ public: virtual void display(); virtual int edit(); virtual bool editable() const; virtual void *data(); virtual Field *clone() const; };
This is what I have in my cpp file:
c++ Syntax (Toggle Plain Text)
// Field Class functions void Field::display(){ } int Field::edit(){ return 0; } bool Field::editable() const{ return false; } void Field::*data(){ return NULL; } void Field::*clone() const{ return NULL; }
I am getting errors with what I currently have and decided to break it down piece by piece. I would like to know if what I did is correct, any input is highly appreciated.
// Star is in the wrong place void Field::*data(){ return NULL; } // Star is in the wrong place // void* is the wrong return type void Field::*clone() const{ return NULL; }
C++ Syntax (Toggle Plain Text)
void *Field::data(){ return NULL; } Field *Field::clone() const{ return NULL; }
On a design note, the entire Field class should be abstract because it's a base class. That saves you the need to implement dummy methods that don't do anything but return a useless value.
A good rule of thumb is to make any base class abstract and only leaf classes concrete. That way you don't have to worry about problems like slicing. Also, if you plan on using Field polymorphically, a virtual destructor is a good idea: C++ Syntax (Toggle Plain Text)
#include <iostream> #include <string> class Field{ public: virtual void display() = 0; virtual int edit() = 0; virtual bool editable() const = 0; virtual void *data() = 0; virtual Field *clone() const = 0; virtual ~Field() = 0 {} }; class TextField: public Field { std::string _text; public: TextField(std::string text): _text(text) {} ~TextField() {} void display() { std::cout << _text << '\n'; } int edit() { return 0; } bool editable() const { return false; } void *data() { return (void*)_text.c_str(); } Field *clone() const { return new TextField(_text); } }; int main() { Field *field1 = new TextField("Ed Rules!"); Field *field2 = field1->clone(); field1->display(); field2->display(); delete field2; delete field1; }
If at first you don't succeed, keep on sucking until you do succeed.
Well No Wonder You are getting errors. This is because you have declared a void function and giving a return value to it.
Thanks for that information! Although when I compile that example program I get an error saying "pure specifier on function definition" on this line:
c++ Syntax (Toggle Plain Text)
virtual ~Field() = 0 {}
Sorry, Edward was accidentally using a Visual Studio extension. This should work:
C++ Syntax (Toggle Plain Text)
#include <iostream> #include <string> class Field{ public: virtual void display() = 0; virtual int edit() = 0; virtual bool editable() const = 0; virtual void *data() = 0; virtual Field *clone() const = 0; virtual ~Field() = 0; }; Field::~Field() {} class TextField: public Field { std::string _text; public: TextField(std::string text): _text(text) {} ~TextField() {} void display() { std::cout << _text << '\n'; } int edit() { return 0; } bool editable() const { return false; } void *data() { return (void*)_text.c_str(); } Field *clone() const { return new TextField(_text); } }; int main() { Field *field1 = new TextField("Ed Rules!"); Field *field2 = field1->clone(); field1->display(); field2->display(); delete field2; delete field1; }
If at first you don't succeed, keep on sucking until you do succeed.
![]() |
Similar Threads
- Help on Creating Class in VB6 (Visual Basic 4 / 5 / 6)
- Creating a Class Library (VB.NET)
- Dynamic class members? (C++)
- Reading an input file as a class memeber function (C++)
- creating a class and declaring objects (C++)
- need help in creating class string (C++)
Other Threads in the C++ Forum
- Previous Thread: how to read a binary file data to a monitor
- Next Thread: 12 errors
| Thread Tools | Search this Thread |
api array based beginner bitmap c++ c/c++ calculator char class classes code coding compile compiler console conversion count database delete deploy desktop developer directshow dll download dynamic email encryption error file forms fstream function functions game givemetehcodez google graph gui homeworkhelp homeworkhelper iamthwee ifstream input int integer java lib linkedlist linker linux list loop looping loops map math matrix memory multiple news node number output parameter pointer problem program programming project python random read recursion recursive return sorting string strings struct temperature template templates test text text-file tree unix url variable vector video visualstudio win32 windows winsock word wordfrequency wxwidgets





