943,832 Members | Top Members by Rank

Ad:
  • C++ Discussion Thread
  • Marked Solved
  • Views: 824
  • C++ RSS
Aug 22nd, 2008
0

Help with creating a class

Expand Post »
Hello everyone I have to create a class that has the following:
Quote ...
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.
I would like some input if I did the above correctly.

This is what is in my header file:

c++ Syntax (Toggle Plain Text)
  1. class Field{
  2. public:
  3. virtual void display();
  4. virtual int edit();
  5. virtual bool editable() const;
  6. virtual void *data();
  7. virtual Field *clone() const;
  8. };

This is what I have in my cpp file:

c++ Syntax (Toggle Plain Text)
  1. // Field Class functions
  2. void Field::display(){
  3.  
  4. }
  5. int Field::edit(){
  6. return 0;
  7. }
  8. bool Field::editable() const{
  9. return false;
  10. }
  11. void Field::*data(){
  12. return NULL;
  13. }
  14. void Field::*clone() const{
  15. return NULL;
  16. }

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.
Similar Threads
Reputation Points: 10
Solved Threads: 2
Junior Poster in Training
edouard89 is offline Offline
53 posts
since Feb 2007
Aug 22nd, 2008
1

Re: Help with creating a class

// 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;
}
The class name and scope operator go right next to the method name, after the return type. You just need to move the star over a bit. In your class you say that clone() returns a Field* too:
C++ Syntax (Toggle Plain Text)
  1. void *Field::data(){
  2. return NULL;
  3. }
  4.  
  5. Field *Field::clone() const{
  6. return NULL;
  7. }
Make sure you include a header that defines NULL, like <cstddef>, or that might cause an error too.

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)
  1. #include <iostream>
  2. #include <string>
  3.  
  4. class Field{
  5. public:
  6. virtual void display() = 0;
  7. virtual int edit() = 0;
  8. virtual bool editable() const = 0;
  9. virtual void *data() = 0;
  10. virtual Field *clone() const = 0;
  11. virtual ~Field() = 0 {}
  12. };
  13.  
  14. class TextField: public Field {
  15. std::string _text;
  16. public:
  17. TextField(std::string text): _text(text) {}
  18. ~TextField() {}
  19.  
  20. void display() { std::cout << _text << '\n'; }
  21. int edit() { return 0; }
  22. bool editable() const { return false; }
  23. void *data() { return (void*)_text.c_str(); }
  24. Field *clone() const { return new TextField(_text); }
  25. };
  26.  
  27. int main()
  28. {
  29. Field *field1 = new TextField("Ed Rules!");
  30. Field *field2 = field1->clone();
  31.  
  32. field1->display();
  33. field2->display();
  34.  
  35. delete field2;
  36. delete field1;
  37. }
Reputation Points: 361
Solved Threads: 97
Posting Pro
Radical Edward is offline Offline
526 posts
since May 2008
Aug 22nd, 2008
0

Re: Help with creating a class

Well No Wonder You are getting errors. This is because you have declared a void function and giving a return value to it.
Reputation Points: 673
Solved Threads: 125
Practically a Posting Shark
Sky Diploma is offline Offline
818 posts
since Mar 2008
Aug 22nd, 2008
0

Re: Help with creating a class

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)
  1. virtual ~Field() = 0 {}
Reputation Points: 10
Solved Threads: 2
Junior Poster in Training
edouard89 is offline Offline
53 posts
since Feb 2007
Aug 22nd, 2008
0

Re: Help with creating a class

Sorry, Edward was accidentally using a Visual Studio extension. This should work:
C++ Syntax (Toggle Plain Text)
  1. #include <iostream>
  2. #include <string>
  3.  
  4. class Field{
  5. public:
  6. virtual void display() = 0;
  7. virtual int edit() = 0;
  8. virtual bool editable() const = 0;
  9. virtual void *data() = 0;
  10. virtual Field *clone() const = 0;
  11. virtual ~Field() = 0;
  12. };
  13.  
  14. Field::~Field() {}
  15.  
  16. class TextField: public Field {
  17. std::string _text;
  18. public:
  19. TextField(std::string text): _text(text) {}
  20. ~TextField() {}
  21.  
  22. void display() { std::cout << _text << '\n'; }
  23. int edit() { return 0; }
  24. bool editable() const { return false; }
  25. void *data() { return (void*)_text.c_str(); }
  26. Field *clone() const { return new TextField(_text); }
  27. };
  28.  
  29. int main()
  30. {
  31. Field *field1 = new TextField("Ed Rules!");
  32. Field *field2 = field1->clone();
  33.  
  34. field1->display();
  35. field2->display();
  36.  
  37. delete field2;
  38. delete field1;
  39. }
Reputation Points: 361
Solved Threads: 97
Posting Pro
Radical Edward is offline Offline
526 posts
since May 2008
Aug 22nd, 2008
0

Re: Help with creating a class

I see thanks!
Reputation Points: 10
Solved Threads: 2
Junior Poster in Training
edouard89 is offline Offline
53 posts
since Feb 2007

This thread is solved

Either the thread starter or a moderator has marked this thread as solved. You can most likely trust the responses and answers given. There is most likely no reason for any further responses to be posted here. If you have a related question, please start a new thread in this forum instead.

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: how to read a binary file data to a monitor
Next Thread in C++ Forum Timeline: 12 errors





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


Follow us on Twitter


© 2011 DaniWeb® LLC