tampurus

11.4 Hierarchical Inheritance

Jan 7th, 2022
116
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 0.61 KB | None | 0 0
  1. #include <iostream>
  2. using namespace std;
  3.  
  4. class Base{
  5. protected:
  6.     int x,y;
  7. public:
  8.     void get()
  9.     {
  10.         cout<<"Enter two numebrs"<<endl;
  11.         cin>>x>>y;
  12.     }
  13. };
  14.  
  15. class D_1 : public Base
  16. {
  17. public:
  18.     void sum()
  19.     {
  20.         cout<<"sum is "<<x+y<<endl;
  21.     }
  22. };
  23.  
  24. class D_2 : public Base{
  25. public:
  26.     void product()
  27.     {
  28.         cout<<"Product is "<<x*y<<endl;
  29.     }
  30. };
  31.  
  32. int main()
  33. {
  34.     D_1 a;
  35.     D_2 b;
  36.     a.get();
  37.     a.sum();
  38.     b.get();
  39.     b.product();
  40.     return 0;
  41. }
  42.  
  43. /*
  44. Output
  45. Enter two numebrs
  46. 4 4
  47. sum is 8
  48. Enter two numebrs
  49. 33 3
  50. Product is 99
  51. */
Advertisement
Add Comment
Please, Sign In to add comment