Help with creating a class

Thread Solved
Reply

Join Date: Feb 2007
Posts: 53
Reputation: edouard89 is an unknown quantity at this point 
Solved Threads: 2
edouard89's Avatar
edouard89 edouard89 is offline Offline
Junior Poster in Training

Help with creating a class

 
0
  #1
Aug 22nd, 2008
Hello everyone I have to create a class that has the following:
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:

  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:

  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.
Reply With Quote Quick reply to this message  
Join Date: May 2008
Posts: 351
Reputation: Radical Edward has a spectacular aura about Radical Edward has a spectacular aura about Radical Edward has a spectacular aura about 
Solved Threads: 62
Radical Edward's Avatar
Radical Edward Radical Edward is offline Offline
Posting Whiz

Re: Help with creating a class

 
1
  #2
Aug 22nd, 2008
// 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:
  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:
  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. }
If at first you don't succeed, keep on sucking until you do succeed.
Reply With Quote Quick reply to this message  
Join Date: Mar 2008
Posts: 672
Reputation: Sky Diploma is a splendid one to behold Sky Diploma is a splendid one to behold Sky Diploma is a splendid one to behold Sky Diploma is a splendid one to behold Sky Diploma is a splendid one to behold Sky Diploma is a splendid one to behold 
Solved Threads: 99
Sky Diploma's Avatar
Sky Diploma Sky Diploma is offline Offline
Practically a Master Poster

Re: Help with creating a class

 
0
  #3
Aug 22nd, 2008
Well No Wonder You are getting errors. This is because you have declared a void function and giving a return value to it.
1. Please Mark Your Thread as Solved After Getting Your Answers.
2. Please Use CODE TAGS .
Reply With Quote Quick reply to this message  
Join Date: Feb 2007
Posts: 53
Reputation: edouard89 is an unknown quantity at this point 
Solved Threads: 2
edouard89's Avatar
edouard89 edouard89 is offline Offline
Junior Poster in Training

Re: Help with creating a class

 
0
  #4
Aug 22nd, 2008
Thanks for that information! Although when I compile that example program I get an error saying "pure specifier on function definition" on this line:

  1. virtual ~Field() = 0 {}
Reply With Quote Quick reply to this message  
Join Date: May 2008
Posts: 351
Reputation: Radical Edward has a spectacular aura about Radical Edward has a spectacular aura about Radical Edward has a spectacular aura about 
Solved Threads: 62
Radical Edward's Avatar
Radical Edward Radical Edward is offline Offline
Posting Whiz

Re: Help with creating a class

 
0
  #5
Aug 22nd, 2008
Sorry, Edward was accidentally using a Visual Studio extension. This should work:
  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. }
If at first you don't succeed, keep on sucking until you do succeed.
Reply With Quote Quick reply to this message  
Join Date: Feb 2007
Posts: 53
Reputation: edouard89 is an unknown quantity at this point 
Solved Threads: 2
edouard89's Avatar
edouard89 edouard89 is offline Offline
Junior Poster in Training

Re: Help with creating a class

 
0
  #6
Aug 22nd, 2008
I see thanks!
Reply With Quote Quick reply to this message  
Reply

This thread has been marked solved.
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