Advertisement
Guest User

Untitled

a guest
Apr 1st, 2015
221
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 0.63 KB | None | 0 0
  1. #include <iostream>
  2.  
  3. class Parent
  4. {
  5. protected:
  6.     int my_num;
  7. public:
  8.     Parent(int my_num)
  9.     {
  10.         this->my_num = my_num;
  11.     }
  12. // virtual
  13.     void eat()
  14.     {
  15.         std::cout << this->my_num << std::endl;
  16.     }
  17. };
  18.  
  19. class Child : Parent
  20. {
  21. public:
  22.     Child(int my_num) : Parent(my_num)
  23.     {
  24.        
  25.     }
  26.  
  27.     void eat()
  28.     {
  29.         std::cout << "child: " << this->my_num << std::endl;
  30.     }
  31. };
  32.  
  33. void dummy_fun(Parent* p)
  34. {
  35.     p->eat();
  36. }
  37.  
  38. int main()
  39. {
  40.     Parent* p = new Parent(4);
  41.     Child* c = new Child(5);
  42.  
  43.     dummy_fun(p);
  44.     dummy_fun((Parent*) c);
  45.  
  46.     return 0;
  47. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement