I have a number of classes that represent various computer components, each of which have an overloaded << operator declared as follows:
friend ostream& operator << (ostream& os, const MotherBoard& mb);
Each returns an ostream object with a unique stream describing that component, some of which are composed of other components. I decided to create a base class called Component in order to generate a unique id as well as some other functions that all the components will publicly derive. Of course, the overloaded << operator doesn’t work with pointers to Component objects.
I was wondering how I would effect something like a pure virtual function that will be overwritten by each derived class’s << operator so I could do something like:
Component* mobo = new MotherBoard();
cout << *mobo << endl;
delete mobo;
Also related to: overloading << operators and inherited classes
Maybe something like this: