next up previous
Next: Example Abstract Base Class Up: C++ Design For Research Previous: RD_LoaderSingleton<T>

Formatting Nested Output

#include "Rad/Indent.h"

class Fred
{
public:
    void print(ostream& os) const { os<<RD_Indent()<<"Fred's data"; }	
};

ostream& operator<<(ostream& os, const Fred& fred)
{
    os<<"Fred: "<<endl;
    RD_Indent(os)++;
    fred.print(os);
    RD_Indent(os)--;
    return os;
}

class Jim
{
private:
    Fred _fred;
public:
    void print(ostream& os) const 
    { os<<RD_Indent()<<_fred<<endl;
      os<<RD_Indent()<<"Jim's other data"; }	
};

ostream& operator<<(ostream& os, const Jim& jim)
{
    os<<"Jim: "<<endl;
    RD_Indent(os)++;
    jim.print(os);
    RD_Indent(os)--;
    return os;
}

main()
{
    Jim jim;
    cout<<jim<<endl;
}
Output:
Jim:
    Fred:
        Fred's data
    Jim's other data



Tim Cootes
2/24/1998