Advertisement
Guest User

Untitled

a guest
Mar 30th, 2017
54
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 0.83 KB | None | 0 0
  1.  
  2.  
  3.  
  4.  
  5.  
  6.  
  7.  
  8. //Object slicing doesn't occur when pointers or references to objects are passed as function arguments since both the pointers are of the same size.
  9.  
  10. #include <iostream>
  11. using namespace std;
  12.  
  13.  
  14.     class Base {
  15.  
  16.     public:
  17.  
  18.         Base(long ss) : ss_num(ss) { }
  19.  
  20.         long ss_num;
  21.         virtual void dispaly()
  22.         {
  23.  
  24.             cout << "base" << ss_num;
  25.  
  26.         }
  27.     };
  28.  
  29.     class Derived : public Base {
  30.  
  31.     public:
  32.  
  33.         Derived(long social, float pay) : Base(social), pay_rate(pay) { }
  34.        
  35.             virtual void dispaly()
  36.         {
  37.  
  38.             cout << "Derived"  << pay_rate << " " << ss_num;
  39.  
  40.         }
  41.         float pay_rate;
  42.     };
  43.  
  44.  
  45.     void test(Base& test)
  46.     {
  47.  
  48.         test.dispaly();
  49.  
  50.     }
  51.  
  52.     int main() {
  53.  
  54.         Base bp(33);
  55.         Derived dp(45, 54);
  56.        
  57.         Base &test = dp;
  58.         bp = dp;
  59.  
  60.         test.dispaly();
  61.         cout << endl;
  62.         bp.dispaly();
  63.        
  64.    
  65.  
  66.         return 0;
  67.     }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement