Advertisement
avr39ripe

inheritance

Jul 21st, 2020
122
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 0.97 KB | None | 0 0
  1. #include <iostream>
  2.  
  3. class Parent
  4. {
  5.     int id;
  6.  
  7. public:
  8.     Parent(int idP)
  9.         : id{ idP }
  10.     { std::cout << "Parent constructed for " << this << " with id = " << id << '\n'; };
  11.     Parent() : Parent{ 0 } {};
  12.     ~Parent() { std::cout << "Parent destructed for " << this << " with id = " << id << '\n'; }
  13.     int getId() const { return id; };
  14.     Parent& setId(int idP) { id = idP; return *this; };
  15. };
  16.  
  17. class Child : public Parent
  18. {
  19.     int val;
  20. public:
  21.     Child(int valP, int idP)
  22.         : Parent{ idP }, val{ valP }
  23.     {
  24.         std::cout << "Child constructed for "
  25.             << this << " with id = " << getId() << " val = " << val << '\n';
  26.     };
  27.     Child() : Child{ 0, 0 } {};
  28.     ~Child()
  29.     {
  30.         std::cout << "Child destructed for "
  31.             << this << " with id = " << getId() << " val = " << val << '\n';
  32.     }
  33. };
  34. int main()
  35. {
  36.     std::cout << "Parent...\n";
  37.     Parent p;
  38.     std::cout << "Child...\n";
  39.     Child c;
  40. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement