SRD

inheritance

SRD
Oct 24th, 2017
304
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 0.72 KB | None | 0 0
  1. class Element {
  2. public:
  3.     virtual void foo() = 0;
  4.     virtual void bar() {}  
  5. };
  6.  
  7. class Point : Element {
  8. private:
  9.     vector<float> coords;
  10.  
  11. public:
  12.     virtual void foo();
  13. };
  14.  
  15. class Quad : Element {
  16. private:
  17.     vector<Point> points;
  18.  
  19. public:
  20.     virtual void foo();
  21. };
  22.  
  23. /*
  24. How does one call a function in a derived class using a pointer to the
  25. base class ... where it doesn't make sense to add the function (set_coords())
  26. to the base (i.e., I don't make it virtual because only the Point class
  27. needs set_coords()).
  28. */
  29.  
  30.     auto elem = ElementFactory::instance()->create("Point");
  31.     elem->set_coords(v);
  32.  
  33. /*
  34. The above would complain that class Element has no member named set_coords(v);
  35. */
Advertisement
Add Comment
Please, Sign In to add comment