Can someone please explain how to ensure that the derived function is called from a pointer of type base to a derived object instead of the base function...
Also, are the virtual and override keywords best practice to accomplish this?
I had previously defined each overload with keyword friend in each class; but the base function was called for the base pointer to derived object.
int main()
{
// contrived example ...
base* ptr_derived = new derived();
std::cout << *ptr_derived;
delete ptr_derived;
}
class base
{
virtual std::ostream& operator<<(std::ostream output)
{
output << // base details...
return output;
}
};
class derived : public base
{
std::ostream& operator<<(std::ostream output) // override?
{
output << // derived details...
return output;
}
};