What is the point in virtual things???

Please support our C++ advertiser: Intel Parallel Studio Home
Thread Solved

Join Date: May 2008
Posts: 51
Reputation: Manutebecker is an unknown quantity at this point 
Solved Threads: 2
Manutebecker's Avatar
Manutebecker Manutebecker is offline Offline
Junior Poster in Training

What is the point in virtual things???

 
0
  #1
Dec 15th, 2008
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
Reply With Quote Quick reply to this message  
Join Date: Jul 2008
Posts: 2,001
Reputation: ArkM has much to be proud of ArkM has much to be proud of ArkM has much to be proud of ArkM has much to be proud of ArkM has much to be proud of ArkM has much to be proud of ArkM has much to be proud of ArkM has much to be proud of ArkM has much to be proud of 
Solved Threads: 343
ArkM's Avatar
ArkM ArkM is offline Offline
Postaholic

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

 
0
  #2
Dec 15th, 2008
Originally Posted by Manutebecker View 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
???!!!
It's a false statement (or I misunderstand the post).
Reply With Quote Quick reply to this message  
Join Date: Oct 2008
Posts: 1,999
Reputation: ddanbe has much to be proud of ddanbe has much to be proud of ddanbe has much to be proud of ddanbe has much to be proud of ddanbe has much to be proud of ddanbe has much to be proud of ddanbe has much to be proud of ddanbe has much to be proud of ddanbe has much to be proud of 
Solved Threads: 297
ddanbe's Avatar
ddanbe ddanbe is offline Offline
Posting Virtuoso

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

 
0
  #3
Dec 15th, 2008
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.
Today is a gift, that's why it is called "The Present".
Make love, no war. Cave ab homine unius libri.
Danny
Reply With Quote Quick reply to this message  
Join Date: Aug 2008
Posts: 206
Reputation: grumpier has a spectacular aura about grumpier has a spectacular aura about 
Solved Threads: 31
grumpier grumpier is offline Offline
Posting Whiz in Training

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

 
2
  #4
Dec 15th, 2008
Originally Posted by ddanbe View Post
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.
  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;
  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;
  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.
Reply With Quote Quick reply to this message  
Join Date: Oct 2008
Posts: 1,999
Reputation: ddanbe has much to be proud of ddanbe has much to be proud of ddanbe has much to be proud of ddanbe has much to be proud of ddanbe has much to be proud of ddanbe has much to be proud of ddanbe has much to be proud of ddanbe has much to be proud of ddanbe has much to be proud of 
Solved Threads: 297
ddanbe's Avatar
ddanbe ddanbe is offline Offline
Posting Virtuoso

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

 
0
  #5
Dec 15th, 2008
Thanks for the clarification.
I saw the forrest but forgot about the trees...
Today is a gift, that's why it is called "The Present".
Make love, no war. Cave ab homine unius libri.
Danny
Reply With Quote Quick reply to this message  
Reply

This thread has been marked solved.
Perhaps start a new thread instead?
Message:



Similar Threads
Other Threads in the C++ Forum
Thread Tools Search this Thread



Tag cloud for C++
About Us | Contact Us | Advertise | DaniWeb | Acceptable Use Policy | RSS Feed

©2003 - 2009 DaniWeb® LLC