Why can I not print the derived object using this approach? How can I fix this so that the derived printout prints "derived size=8". I ideally want to keep the printing code out of the class code.

```
#include <iostream>

class base
{
public:
   base(int size) : size_(size) {}

   virtual int get_size() const { return size_; }

private:
   int size_;
};

std::ostream& operator<<(std::ostream& os, const base& obj) {
   os << "base size=" << obj.get_size() << std::endl;
   return os;
}

class derived1 : public base
{
public:
   derived1(int size) : base(size) {}

   virtual int get_size() const { 
      return 2 * base::get_size();
   }
};

std::ostream& operator<<(std::ostream& os, const derived1& obj) {
   os << "derived1 size=" << obj.get_size() << std::endl;
   return os;
}

int main(int argc, char* argv[]) {

   base* b1 = new base(3);
   std::cout << "b1 size is: " << b1->get_size() << std::endl;

   std::cout << *b1 << std::endl;

   base* b2 = new derived1(4);
   std::cout << "b2 size is: " << b2->get_size() << std::endl;

   std::cout << *b2 << std::endl;

   delete b1;
   delete b2;
   return 0;
}

Output:
b1 size is: 3
base size=3

b2 size is: 8
base size=8

```

UPDATE:

I changed as follows as per ghostsofstandardspast which works:

```
#include <iostream>

class base
{
public:
   base(int size) : size_(size) {}

   virtual int get_size() const { return size_; }
   friend std::ostream& operator<<(std::ostream &os, const base& obj);

private:
   virtual std::ostream &print(std::ostream &os) const {
      return os << "base size=" << get_size() << std::endl;
   }

   int size_;
};

std::ostream& operator<<(std::ostream& os, const base& obj) {
   obj.print(os);
   return os;
}

class derived1 : public base
{
public:
   derived1(int size) : base(size) {}

   virtual int get_size() const { 
      return 2 * base::get_size();
   }

   friend std::ostream& operator<<(std::ostream &os, const derived1& obj);
private:
   virtual std::ostream &print(std::ostream &os) const {
      return os << "derived1 size=" << get_size() << std::endl;
   }
};

int main(int argc, char* argv[]) {

   base* b1 = new base(3);
   std::cout << "b1 size is: " << b1->get_size() << std::endl;

   std::cout << *b1 << std::endl;

   base* b2 = new derived1(4);
   std::cout << "b2 size is: " << b2->get_size() << std::endl;

   std::cout << *b2 << std::endl;
   return 0;
}

```