avr39ripe

cppOperator<<OverloadTask

Aug 19th, 2021
1,082
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. class Parent
  2. {
  3.     int parentId;
  4. public:
  5.     Parent(int parentIdP) : parentId{parentIdP} {}
  6.     virtual void info() { std::cout << "I'm a Parent with pId = " << parentId << '\n'; }
  7.     virtual ~Parent(){ std::cout << "Parent dtor\n"; }
  8. };
  9.  
  10. //Parent::~Parent()
  11. //{
  12. //  std::cout << "Parent dtor\n";
  13. //}
  14.  
  15. class Child : public Parent
  16. {
  17.     int size;
  18.     int* arr;
  19. public:
  20.     Child(int sizeP) : Parent(42), size{ sizeP }, arr{ new int[sizeP]} {}
  21.     virtual void info()override { std::cout << "I'm a Child with size = " << size << '\n'; }
  22.     ~Child() { delete[] arr; std::cout << "Child dtor\n"; }
  23. };
  24.  
  25.  
  26. int main()
  27. {
  28.     //{
  29.     //  Parent p{ 33 };
  30.     //  Child c{ 10 };
  31.  
  32.     //  p.info();
  33.     //  c.info();
  34.     //}
  35.    
  36.     {
  37.         Parent* pPtr1{ new Parent{333} };
  38.         std::cout << "This is Parent: ";
  39.         pPtr1->info();
  40.  
  41.         Parent* pPtr2{ new Child{5} };
  42.         std::cout << "This is Child: ";
  43.         pPtr2->info();
  44.  
  45.         delete pPtr1;
  46.         delete pPtr2;
  47.     }
RAW Paste Data