943,754 Members | Top Members by Rank

Ad:
  • C++ Discussion Thread
  • Marked Solved
  • Views: 737
  • C++ RSS
Dec 15th, 2008
0

What is the point in virtual things???

Expand Post »
After reading up on them in terms of class hierarchy, they seem to just be almost like comments, to remind you that all classes use this sort of function
Similar Threads
Reputation Points: 10
Solved Threads: 3
Junior Poster in Training
Manutebecker is offline Offline
51 posts
since May 2008
Dec 15th, 2008
0

Re: What is the point in virtual things???

After reading up on them in terms of class hierarchy, they seem to just be almost like comments, to remind you that all classes use this sort of function
???!!!
It's a false statement (or I misunderstand the post).
Reputation Points: 1234
Solved Threads: 347
Postaholic
ArkM is offline Offline
2,001 posts
since Jul 2008
Dec 15th, 2008
0

Re: What is the point in virtual things???

When you mark a method of your class as virtual you give the user of your class the possibility to override that method. If you don't, the metod can't be overridden.
Reputation Points: 2035
Solved Threads: 644
Senior Poster
ddanbe is offline Offline
3,736 posts
since Oct 2008
Dec 15th, 2008
2

Re: What is the point in virtual things???

Click to Expand / Collapse  Quote originally posted by ddanbe ...
When you mark a method of your class as virtual you give the user of your class the possibility to override that method. If you don't, the metod can't be overridden.
That statement is inaccurate on so many levels that I'm speechless. Non-virtual functions can also be overridden, but the behaviour differs from overridden virtual functions due to "name hiding", as described in the C++ standard.

The point of virtual member functions is that they allow functionality to be specified in a base class, and specialised in a derived class. For example, a generic Shape class might declare a virtual function named Area() to compute it's area, and another virtual function called Name(). Derived classes (which represent particular shapes) override those virtual function to give appropriate results for particular shapes.
C++ Syntax (Toggle Plain Text)
  1. class Shape
  2. {
  3. public:
  4. Shape();
  5. virtual double Area() const = 0;
  6. virtual std::string Name() const = 0;
  7. };
  8.  
  9. class Triangle: public Shape
  10. {
  11. public:
  12. Triangle(double h, double w) : height(h), width(w) {};
  13. double Area() const {return 0.5*height*width;};
  14.  
  15. std::string Name() const {return "Triangle";}
  16.  
  17. private:
  18. double height, width;
  19. };
  20.  
  21. class Circle : public Shape
  22. {
  23. public:
  24. Circle(double r): radius(r) {};
  25. virtual double Area() const {return 4.0*atan(1.0)*radius*radius;};
  26. virtual std::string Name() const {return "Circle";}
  27. private:
  28. double radius;
  29. };
Now, let's say we have a function that only knows about the Shape class, and wishes to print its name and area. For example;
C++ Syntax (Toggle Plain Text)
  1. void PrintArea(const Shape &s)
  2. {
  3. std::cout << s.Name() << " - Area = " << s.Area() << '\n';
  4. }
This function can be passed any Shape object by reference, and will print out information about the actual shape passed.

This is described as polymorphic behaviour: the class Shape is a polymorphic base class, derived classes modify it's behaviour, but the interface seen by the PrintArea() function is the same for all the classes. The PrintArea() function only receives generic information about a Shape (i.e. it provides a Name() and Area() member function) but, at run time, the behaviours correspond to the actual type of the object passed.

For example;
C++ Syntax (Toggle Plain Text)
  1. int main()
  2. {
  3. Triangle tri(2.0, 4.0);
  4. Circle cir(5.0);
  5.  
  6. PrintArea(tri); // will print "Triangle - Area = 4"
  7. PrintArea(cir); // will print "Circle - Area = 78.5" (possibly higher precision value)
  8.  
  9. Shape *s[2];
  10. s[1] = &cir;
  11. s[0] = &tri;
  12.  
  13. // this loop will print out the above lines of output in reverse order
  14. for (int i = 0; i < 2; ++i)
  15. PrintArea(s[i]);
  16. }
Last edited by grumpier; Dec 15th, 2008 at 4:07 am.
Reputation Points: 193
Solved Threads: 32
Posting Whiz in Training
grumpier is offline Offline
206 posts
since Aug 2008
Dec 15th, 2008
0

Re: What is the point in virtual things???

Thanks for the clarification.
I saw the forrest but forgot about the trees...
Reputation Points: 2035
Solved Threads: 644
Senior Poster
ddanbe is offline Offline
3,736 posts
since Oct 2008

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: deactivate JIT debugger
Next Thread in C++ Forum Timeline: C++ help :(





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


Follow us on Twitter


© 2011 DaniWeb® LLC