Q: Derived Members and Overloaded Ostream Operator Working with a large number of derived classes whose members I would like to display with the << operator. As more classes are used it will likely become quite cumbersome to override the operator/other functions for the new members. I was curious if there was a way to accomplish something similar to the following? Which of course, will overflow.
// Base class
class Foo {
public:
    friend ostream& operator <<(ostream& outputStream, const Foo& foo) {
        outputStream << foo.fooName + ": " + foo.fooInfo + ", ";
        return outputStream;
    }
protected:
    string fooName;
    string fooInfo;
};

// Foo Derived class
class FooBar : public Foo {
public:
    friend ostream& operator <<(ostream& outputStream, const FooBar& fooBar) {
        outputStream << fooBar; // (fooBar.fooName + ": " + fooBar.fooInfo + ", ")
        outputStream << fooBar.barName + ": " + fooBar.barInfo + ", ";
        return outputStream;
        /* The above as opposed to:
        outputStream << fooBar.fooName + ": " + fooBar.fooInfo + ", " <<
            fooBar.barName + ": " + fooBar.barInfo + ", "; */
    }
protected:
    string barName;
    string barInfo;
};

If something similar (recursive) is not possible, what is an alternative solution? IE passing different argument(s) or calling an ostream/other function return within the operator body. Thanks.

A: This may help you out, however the Base class In this case is completely Abstract! Here are the Base & 2 Derived Classes
#include <conio.h>
#include <string>
#include <iostream>

class Base {
    friend std::ostream& operator<<( std::ostream& out, const Base* const pObj );

private:
    std::string m_strName;

public: 
    std::string getName() const { return m_strName; }
    virtual ~Base(){} // Default Okay 

protected:
    explicit Base( std::string strName ) : m_strName( strName ) {}

    virtual void printData( std::ostream& out ) const = 0;

}; 

std::ostream& operator<<( std::ostream& out, const Base* const pObj ) {
    out << pObj->m_strName << std::endl;
    pObj->printData( out ); 
    return out;
}

class DerivedA : public Base {
private:
    int m_value;

public:
    explicit DerivedA( int& a ) : Base( std::string( "DerivedA" ) ), m_value( a ) {}

protected:
    virtual void printData( std::ostream& out ) const override;
};

void DerivedA::printData( std::ostream& out ) const {
    out << "Value: " << m_value;
}

class DerivedB : public Base {
private:
    float m_value;

public:
    explicit DerivedB( float& f ) : Base( std::string( "DerivedB" ) ), m_value( f ) {}

protected:
    virtual void printData( std::ostream& out ) const override;
};

void DerivedB::printData( std::ostream& out ) const {
    out << "Value: " << m_value;
}

int main () {

    int a = 4;
    float b = 3.2f;

    DerivedA derivedA( a );
    DerivedB derivedB( b );

    // Notice I Used The Address Of Operator Here Due To How The << operator Is Defined
    std::cout << &derivedA << std::endl << std::endl;
    std::cout << &derivedB << std::endl << std::endl;

    std::cout << "Press any key to quit"  << std::endl;
    _getch();
    return 0;
} 

Another way to use this instead of on the stack would be on the heap but make sure to clean up all memory.
int main () {

    int a = 4;
    float b = 3.2f;

    DerivedA derivedA = new DerivedA( a );
    DerivedB derivedB = new DerviedB( b );

    // Since This Is On The Heap I Can Use The Class Directly
    std::cout << derivedA << std::endl << std::endl;
    std::cout << derivedB << std::endl << std::endl;

    // Make Sure To Clean Up Memory
    delete derivedA;
    delete derivedB;

    std::cout << "Press any key to quit"  << std::endl;
    _getch();
    return 0;
} 

What I ended up doing here is I created a friend << operator in the Base Class that will call a printData() function that is defined as being a purely virtual method. Remember that you can not construct a Base object directly since it is completely Abstract. Here each Derived type will have to implement its own printData() function! This way each class will know how to use the << operator since it is defined in the base class that it is inherited from. This is the power of Inheritance. So now I only have to defined the overloaded operator once and can use it for every derived type, but all derived types must specify a printData() function since each derived type may have different internal data that you would like to have printed. 
If you do not want to use the pointer notation as I have defined within my Base Class << operator declaration & definition you can do it this way as well:
class Base {
    friend std::ostream& operator<<( std::ostream& out, const Base& obj );

    // Everything Else Is The Same As Before
};

std::ostream& operator<<( std::ostream& out, const Base& obj ) {
    out << obj.m_strName << std::endl;
    obj.printData( out );
}

int main() {

    // Everything Is Same As Before As In The First main() example
    // Except For The Two std::cout Statements Where As Here You
    // Can Use The Objects Directly Instead Of Using The Address Of Operator
    std::cout << derivedA << std::endl;
    std::cout << derivedB << std::endl;

    // Rest Is Same As Before.
}

If you want you can include both versions in your base class so that the stream operator << knows how to print your class either by object, reference or pointer!
