tampurus

11.5 Hybrid Inheritance

Jan 7th, 2022
105
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 0.45 KB | None | 0 0
  1. #include <iostream>
  2. using namespace std;
  3.  
  4. class A{
  5. protected:
  6.     int x;
  7. };
  8.  
  9. class B : public A{
  10. public:
  11.     B()
  12.     {
  13.         x = 5;
  14.     }
  15. };
  16.  
  17. class C {
  18. protected:
  19.     int y;
  20. public:
  21.     C()
  22.     {
  23.         y = 8;
  24.     }
  25. };
  26.  
  27. class D : public B,C {
  28. public:
  29.     void sum ()
  30.     {
  31.         cout<<"sum is "<<x+y<<endl;
  32.     }
  33. };
  34.  
  35. int main()
  36. {
  37.     D obj; // created object of D class
  38.     obj.sum();
  39.     return 0;
  40. }
  41. // sum is 13
Advertisement
Add Comment
Please, Sign In to add comment