Pastebin launched a little side project called VERYVIRAL.com, check it out ;-) Want more features on Pastebin? Sign Up, it's FREE!

Inheritance

By: chunkyguy on Jun 20th, 2013  |  syntax: C++  |  size: 1.05 KB  |  views: 40  |  expires: Never
download  |  raw  |  embed  |  report abuse  |  print
Text below is selected. Please press Ctrl+C to copy to your clipboard. (⌘+C on Mac)
  1. /* OUTPUT
  2. ... Draw ImageView ...
  3. View::render
  4. ImageView::render
  5. ... Draw Sprite ...
  6. View::render
  7. ImageView::render
  8. Sprite::render
  9. */
  10.  
  11. ////////// View ///////////////
  12. class View{
  13. public:
  14.         void Render(){
  15.                 render();
  16.         }
  17. protected:
  18.         virtual void render(){
  19.                 std::cout << "View::render" << std::endl;
  20.         };
  21. };
  22. ////////// ImageView ///////////////
  23. class ImageView : public View{
  24. public:
  25. protected:
  26.         virtual void render(){
  27.                 View::render();
  28.                 std::cout << "ImageView::render" << std::endl;
  29.         }
  30. };
  31. ////////// Sprite ///////////////
  32. class Sprite : public ImageView{
  33. public:
  34. protected:
  35.         virtual void render(){
  36.                 ImageView::render();
  37.                 std::cout << "Sprite::render" << std::endl;
  38.         }
  39. };
  40. ////////// Utility ///////////////
  41. void Draw(View *vw){
  42.         vw->Render();
  43. }
  44. ////////// main ///////////////
  45. int main(){
  46.  
  47.         std::cout << "... Draw ImageView ..." << std::endl;
  48.         ImageView *imgvw = new ImageView;
  49.         Draw(imgvw);
  50.         delete imgvw;
  51.  
  52.         std::cout << "... Draw Sprite ..." << std::endl;
  53.         Sprite *sp = new Sprite;
  54.         Draw(sp);
  55.         delete sp;
  56.        
  57.         return 0;
  58. }