Good afternoon,

Does anyone know whether it's possible to have more than one overloaded ostream in a class?

This is for a time class that i've created however it ouputs data like this 12:12:12:AM, i'd like to use the same ostream operator to output military time 14:12:12, however when I use the ostream operator to output, I'm getting 14:12:12 0. I'd like not to have the extra zero for the military time.

Thank you

ostream &operator <<( ostream& retOut,  const TimeClass  &regtime)
       retOut << setfill( '0' ) << setw( 2 )<< regtime.hours<<":"<< setw( 2 )<<regtime.mins<<":"<< setw( 2 )<<regtime.secs<<" "<<regtime.ap<<endl ;
       return retOut;

The solution that comes to mind right off would be inherited classes with overloaded functions (you may not have reached these topics yet in your coursework).

If you have, just do


class Time

overload <<

... class MilTime

overload <<

... class StandTime

overload <<

It's really not possible within a single class because of how overloading works. But, as Duki suggested, you can probably do it with inheritance.

Thanks for the quick responses,

I ended up creating a display function for military time.

I've actually used an inherited ostream for yet another time display 0300. This is with my TimeChild class.


If I may suggest another solution that you might find more elegant and that could serve other purposes for this class or another:

class MyTime {
    //create formatting containers:
    class MilitaryTime {
      const MyTime& obj;
        explicit MilitaryTime(const MyTtime& aObj) : obj(aObj) { };
        //implement ostream overload as you wish, using obj.
        friend ostream& operator <<(ostream&, const MilitaryTime&);
    class MilitaryTimeNoColon {
      const MyTime& obj;
        explicit MilitaryTimeNoColon(const MyTtime& aObj) : obj(aObj) { };
        //implement ostream overload as you wish, using obj.
        friend ostream& operator <<(ostream&, const MilitaryTimeNoColon&);
    //now, you can have a default format:
    friend ostream& operator <<(ostream&, const MyTime&);
    //and provide formatting constructor functions, if you wish (this is optional)
    MilitaryTime inMilitary() const { return MilitaryTime(*this); };
    MilitaryTimeNoColon inMilitaryNoColon() const { return MilitaryTimeNoColon(*this); };

int main() {
  //now say you have some time variable:
  MyTime current_time = ...;
  //you can print it in default format:
  cout << current_time << endl;
  //you can print it in military format, in either way:
  cout << current_time.inMilitary() << endl;
  cout << MyTime::MilitaryTime(current_time) << endl;
  //and the same for no-colon format:
  cout << current_time.inMilitaryNoColon() << endl;
  cout << MyTime::MilitaryTimeNoColon(current_time) << endl;

This kind of scheme is an easy way to get around having to create inheritance with multiple classes when the only functional difference between them is trivial (such as just an output format).

Thank you,

I'm going to study your example to see whether I can fully understand what's being done. It appears that you have 3 classes without using inheritance. We've not had examples in my class where you can have multiple classes without creating a new file.